note 52512 added to ref.simplexml
| From: | TCHASSAGNETTEt_chassagnette at yahoo dot fr | Date: | Wed, 04 May 2005 08:57:21 +0000 |
| Subject: | note 52512 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88902@lists.php.net to get a copy of this message | ||
Another method to parse an XML Document into a PHP array with SIMPLEXML inspired from Daniel FAIVRE
!
function xml2php($xml)
{
$fils = 0;
$tab = false;
$array = array();
foreach($xml->children() as $key => $value)
{
$child = xml2php($value);
//To deal with the attributes
foreach($node->attributes() as $ak=>$av)
{
$child[$ak] = (string)$av;
}
//Let see if the new child is not in the array
if($tab==false && in_array($key,array_keys($array)))
{
//If this element is already in the array we will create an indexed array
$tmp = $array[$key];
$array[$key] = NULL;
$array[$key][] = $tmp;
$array[$key][] = $child;
$tab = true;
}
elseif($tab == true)
{
//Add an element in an existing array
$array[$key][] = $child;
}
else
{
//Add a simple element
$array[$key] = $child;
}
$fils++;
}
if($fils==0)
{
return (string)$xml;
}
return $array;
}
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52512
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52512&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52512&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52512&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52512&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52512&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52512&report=yes
Search -- http://master.php.net/manage/user-notes.php