PHPverse 2025

Voting

The Note You're Voting On

Sanados at failure dot at
17 years ago
appended to
brian dot reynolds at risaris dot com
20-Feb-2007 10:09

when you got variable nodes at start you array fails and looses nodes beneath.
solution that counts occurance though eats up performance:

function xmlToArray($n)
{
$xml_array = array();
$occurance = array();

foreach($n->childNodes as $nc)
{
$occurance[$nc->nodeName]++;
}

foreach($n->childNodes as $nc){
if( $nc->hasChildNodes() )
{
if($occurance[$nc->nodeName] > 1)
{
$xml_array[$nc->nodeName][] = xmlToArray($nc);
}
else
{
$xml_array[$nc->nodeName] = xmlToArray($nc);
}
}
else
{
return utf8_decode($nc->nodeValue);
}
}
return $xml_array;
}

<< Back to user notes page

To Top