note 100568 deleted from simplexmlelement.children by danbrown

From: Date: Sun, 24 Oct 2010 03:25:32 +0000
Subject: note 100568 deleted from simplexmlelement.children by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173409@lists.php.net to get a copy of this message
Note Submitter: jmpoder at hotmail dot com ---- Another XML to Array Function function xml2array($data, $first = true){ $arr = Array(); $children = $data->children(); $childs = count($children); if($childs > 0){ foreach($children as $child){ if($first){ $arr[$data->getName()][$child->getName()] = xml2array($child, false); }else{ $arr[$child->getName()] = xml2array($child, false); } } }else{ $arr = (string)$data; } return $arr; } $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <label1> <label2>Some Data 1</label2> <label3> <label4> <label5>Some Data 2</label5> </label4> <label6>Some Data 3</label6> </label3> </label1> XML; $data = simplexml_load_string($xml); print_R(xml2array($data)); Output: Array( [label1] => Array( [label2] => Some Data 1 [label3] => Array( [label4] => Array( [label5] => Some Data 2 ) [label6] => Some Data 3 ) ) )

« previous php.notes (#173409) next »