note 52512 modified in ref.simplexml by danbrown

From: Date: Mon, 18 May 2009 01:28:01 +0000
Subject: note 52512 modified in ref.simplexml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155540@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 ! <?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; } ?> --was-- 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; } http://php.net/manual/en/ref.simplexml.php

« previous php.notes (#155540) next »