Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228
Exporting a SimpleXMLElement to an array

Comments Blog About Development Research Sites

Exporting a SimpleXMLElement to an array

Dec 16, 2007
Everyone who has had some doing with XML in PHP's ought to know this nifty object called simpleXMLelement. It's a simple way to manipulate XML elements and retrieve data from them.

However, there has always been one thing I missed in its functionality: the ability to export that data into an array using a single easy command. So I figured I'd write my own :)

Code (php) (nieuw venster):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class EasyXMLElement extends SimpleXMLElement {

  public
final function asArray (SimpleXMLElement $element = NULL) {
      
      // If no element is given, use itself as the element to parse:
    if($element === NULL)
      $element = $this;

      // Parse this elements' value and name:
    $xmlArray['value'] = (string) $element;
    $xmlArray['name']  = (string) $element -> getName();

      // Parse attributes:
    foreach($element -> attributes() as $attributeName => $attributeValue)
      $xmlArray['attributes'][$attributeName] = (string) $attributeValue;

      // Get all children names to check if they're unique,
      // ignoring the notice about index not existing:

    foreach($element -> children() as $childName => $child)
      @
$unique[$childName] = $unique[$childName] === true ? false : true;
      
      // Now once we know which children are unique and which aren't,
      // it's time to parse and assign them:

    foreach($element -> children() as $childName => $child) {
      if(
$unique[$childName] === true)
        $xmlArray[$childName]   = $this -> asArray($child);
      else

        $xmlArray[$childName][] = $this -> asArray($child);
    }
    return
$xmlArray;
  }
}


The difficulty with parsing XML into a PHP array are the limitations of PHP's arrayelements: they can only have children or values, not both and definately not attributes. There are basicly two ways to solve this: 1. ignore PHP's incapabilities and simply discard information that can't be stored, or 2. store extra information in extra array elements. Obviously I've chosen for option 2 here: each element has keys 'name' and 'value' and, if applicable, 'attributes'. It's not the most beautifull sollution, but it tends to work ;)

FragFrog out!

Jan 26, 2008 Matthijs

Note that this function is depricated, actual workplace conditions showed me there were some bugs in it, as well as better ways of handling the data.

Leave a comment if you're interested in my latest version :)

Mar 3, 2008 daniel!

Wel I am interested in the latest version. :)

Ps. This layout is quite broken in Safari.

Mar 3, 2008 Matthijs

Aye, I noticed, I wrote this blog years ago and obviously things have changed a bit =]

I'll post the updated version in a moment :)

Jun 19, 2008 Matthijs

Updated version:

http://www.fragfrog.nl/nutwork/EasyXMLElement.class.zip

Nov 26, 2009 John

there is an error in line 60, it should be:

if (empty($xmlArray))
$xmlArray = $elementValue;

instead of

if (!empty($elementValue))
$xmlArray = $elementValue;

New comment

Your name:
Comment: