note 91032 modified in function.simplexml-element-children by danbrown
| From: | danbrown@php.net | Date: | Thu, 21 May 2009 15:36:22 +0000 |
| Subject: | note 91032 modified in function.simplexml-element-children by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155852@lists.php.net to get a copy of this message | ||
Transform xml to array php
<?php
function xml2phpArray($xml,$arr){
$iter = 0;
foreach($xml->children() as $b){
$a = $b->getName();
if(!$b->children()){
$arr[$a] = trim($b[0]);
}
else{
$arr[$a][$iter] = array();
$arr[$a][$iter] = xml2phpArray($b,$arr[$a][$iter]);
}
$iter++;
}
return $arr;
}
$Array = simplexml_load_string(file_get_contents('myfile.xml'));
print_r(xml2phpArray($Array,array()));
?>
--was--
Transform xml to array php
function xml2phpArray($xml,$arr){
$iter = 0;
foreach($xml->children() as $b){
$a = $b->getName();
if(!$b->children()){
$arr[$a] = trim($b[0]);
}
else{
$arr[$a][$iter] = array();
$arr[$a][$iter] = xml2phpArray($b,$arr[$a][$iter]);
}
$iter++;
}
return $arr;
}
$Array = simplexml_load_string(file_get_contents('myfile.xml'));
print_r(xml2phpArray($Array,array()));
http://php.net/manual/en/function.simplexml-element-children.php