- print_r: documentation ( source)
<?php
$xml = <<<XML
<cats>
<cat>
<name>Jack</name>
<age>2</age>
<color>grey</color>
<color>white</color>
</cat>
<cat>
<name>Maxwell</name>
<age>12</age>
<color>orange</color>
<color>black</color>
</cat>
</cats>
XML;
function xml2array($fname){
$sxi = new SimpleXmlIterator($fname, null, true);
return sxiToArray($sxi);
}
function sxiToArray($sxi){
$a = array();
for( $sxi->rewind(); $sxi->valid(); $sxi->next() ) {
if(!array_key_exists($sxi->key(), $a)){
$a[$sxi->key()] = array();
}
if($sxi->hasChildren()){
$a[$sxi->key()][] = sxiToArray($sxi->current());
}
else{
$a[$sxi->key()][] = strval($sxi->current());
}
}
return $a;
}
// Read cats.xml and print the results:
$catArray = xml2array($xml);
print_r($catArray);
?>