note 100568 added to simplexmlelement.children
| From: | jmpoder at hotmail dot com | Date: | Sat, 23 Oct 2010 22:52:47 +0000 |
| Subject: | note 100568 added to simplexmlelement.children | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173408@lists.php.net to get a copy of this message | ||
Another XML to Array Function
function xml2array($data, $first = true){
$arr = Array();
$children = $data->children();
$childs = count($children);
if($childs > 0){
foreach($children as $child){
if($first){
$arr[$data->getName()][$child->getName()] = xml2array($child, false);
}else{
$arr[$child->getName()] = xml2array($child, false);
}
}
}else{
$arr = (string)$data;
}
return $arr;
}
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<label1>
<label2>Some Data 1</label2>
<label3>
<label4>
<label5>Some Data 2</label5>
</label4>
<label6>Some Data 3</label6>
</label3>
</label1>
XML;
$data = simplexml_load_string($xml);
print_R(xml2array($data));
Output:
Array(
[label1] => Array(
[label2] => Some Data 1
[label3] => Array(
[label4] => Array(
[label5] => Some Data 2
)
[label6] => Some Data 3
)
)
)
----
Server IP: 69.147.83.197
Probable Submitter: 66.201.172.64
----
Manual Page -- http://www.php.net/manual/en/simplexmlelement.children.php
Edit -- https://master.php.net/note/edit/100568
Del: integrated -- https://master.php.net/note/delete/100568/integrated
Del: useless -- https://master.php.net/note/delete/100568/useless
Del: bad code -- https://master.php.net/note/delete/100568/bad+code
Del: spam -- https://master.php.net/note/delete/100568/spam
Del: non-english -- https://master.php.net/note/delete/100568/non-english
Del: in docs -- https://master.php.net/note/delete/100568/in+docs
Del: other reasons-- https://master.php.net/note/delete/100568
Reject -- https://master.php.net/note/reject/100568
Search -- https://master.php.net/manage/user-notes.php