note 98054 modified in book.simplexml by thiago

From: Date: Mon, 21 Mar 2011 13:55:15 +0000
Subject: note 98054 modified in book.simplexml by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-178042@lists.php.net to get a copy of this message
Simple XML in an array of strings without objects: <?php function xml2array($xml) { $arXML=array(); $arXML['name']=trim($xml->getName()); $arXML['value']=trim((string)$xml); $t=array(); foreach($xml->attributes() as $name => $value){ $t[$name]=trim($value); } $arXML['attr']=$t; $t=array(); foreach($xml->children() as $name => $xmlchild) { $t[$name][]=xml2array($xmlchild); //FIX : For multivalued node } $arXML['children']=$t; return($arXML); } ?> [EDITOR thiago NOTE: Function has fixes and improvements by n dot dhuygelaere at oieau dot fr] --was-- Simple XML in an array of strings without objects: <?php function xml2array($xml) { $arXML=array(); $arXML['name']=trim($xml->getName()); $arXML['value']=trim((string)$xml); $t=array(); foreach($xml->attributes() as $name => $value) $t[$name]=trim($value); $arXML['attr']=$t; $t=array(); foreach($xml->children() as $name => $xmlchild) $t[$name]=xml2array($xmlchild); $arXML['children']=$t; return($arXML); } $xml = simplexml_load_file('file.xml'); echo '<pre>'; print_r(xml2array($xml)); echo '</pre>'; ?> http://php.net/manual/en/book.simplexml.php

« previous php.notes (#178042) next »