PHP 8.5.0 Alpha 4 available for testing

Voting

: max(eight, six)?
(Example: nine)

The Note You're Voting On

T CHASSAGNETTE t_chassagnette at yahoo dot fr
20 years ago
Another method to parse an XML Document into a PHP array with SIMPLEXML inspired from Daniel FAIVRE !

<?php
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;

}
?>

<< Back to user notes page

To Top