note 101063 modified in book.simplexml by danbrown

From: Date: Wed, 24 Nov 2010 01:43:43 +0000
Subject: note 101063 modified in book.simplexml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174307@lists.php.net to get a copy of this message
Here's a quick way to dump the nodeValues from SimpleXML into an array using the path to each nodeValue as key. The paths are compatible with e.g. DOMXPath. I use this when I need to update values externally (i.e. in code that doesn't know about the underlying xml). Then I use DOMXPath to find the node containing the original value and update it. <?php function XMLToArrayFlat($xml, &$return, $path='', $root=false) { $children = array(); if ($xml instanceof SimpleXMLElement) { $children = $xml->children(); if ($root){ // we're at root $path .= '/'.$xml->getName(); } } if ( count($children) == 0 ){ $return[$path] = (string)$xml; return; } $seen=array(); foreach ($children as $child => $value) { $childname = ($child instanceof SimpleXMLElement)?$child->getName():$child; if ( !isset($seen[$childname])){ $seen[$childname]=0; } $seen[$childname]++; XMLToArrayFlat($value, $return, $path.'/'.$child.'['.$seen[$childname].']'); } } ?> Use like this: <?php $xml = simplexml_load_string(...some xml string...); $xmlarray = array(); // this will hold the flattened data XMLToArrayFlat($xml, $xmlarray, '', true); ?> You can also pull multiple files in one array: <?php foreach($files as $file){ $xml = simplexml_load_file($file); XMLToArrayFlat($xml, $xmlarray, $file.':', true); } ?> The respective filename/path is thus prefixed to each key. --was-- Here's a quick way to dump the nodeValues from SimpleXML into an array using the path to each nodeValue as key. The paths are compatible with e.g. DOMXPath. I use this when I need to update values externally (i.e. in code that doesn't know about the underlying xml). Then I use DOMXPath to find the node containing the original value and update it. function XMLToArrayFlat($xml, &$return, $path='', $root=false) { $children = array(); if ($xml instanceof SimpleXMLElement) { $children = $xml->children(); if ($root){ // we're at root $path .= '/'.$xml->getName(); } } if ( count($children) == 0 ){ $return[$path] = (string)$xml; return; } $seen=array(); foreach ($children as $child => $value) { $childname = ($child instanceof SimpleXMLElement)?$child->getName():$child; if ( !isset($seen[$childname])){ $seen[$childname]=0; } $seen[$childname]++; XMLToArrayFlat($value, $return, $path.'/'.$child.'['.$seen[$childname].']'); } } Use like this: $xml = simplexml_load_string(...some xml string...); $xmlarray = array(); // this will hold the flattened data XMLToArrayFlat($xml, $xmlarray, '', true); You can also pull multiple files in one array: foreach($files as $file){ $xml = simplexml_load_file($file); XMLToArrayFlat($xml, $xmlarray, $file.':', true); } The respective filename/path is thus prefixed to each key. http://php.net/manual/en/book.simplexml.php

« previous php.notes (#174307) next »