PHP: Convert XML to an array

If you have XML file which you want to convert to an array to get the attributes of an array so this function will helpful for doing this.

XML file:

<books x='600' y='400'>
    <book x='200' y='150'>
        <title x='10' y='10'></title>
        <author x='20' y='20'></author>
    </book>
    <book x='400' y='300'>
        <title x='10' y='10'></title>
        <author x='20' y='20'></author>
    </book>
    <book x='600' y='400'>
        <title x='10' y='10'></title>
        <author x='20' y='20'></author>
    </book>
</books>

you will get an array like this with their attributes:

Array
(
    [book] => Array
        (
            [0] => Array
                (
                    [title] => Array
                        (
                            [@attributes] => Array
                                (
                                    [x] => 10
                                    [y] => 10
                                )

                        )

                    [author] => Array
                        (
                            [@attributes] => Array
                                (
                                    [x] => 20
                                    [y] => 20
                                )

                        )

                    [@attributes] => Array
                        (
                            [x] => 200
                            [y] => 150
                        )

                )

            [1] => Array
                (
                    [title] => Array
                        (
                            [@attributes] => Array
                                (
                                    [x] => 10
                                    [y] => 10
                                )

                        )

                    [author] => Array
                        (
                            [@attributes] => Array
                                (
                                    [x] => 20
                                    [y] => 20
                                )

                        )

                    [@attributes] => Array
                        (
                            [x] => 400
                            [y] => 300
                        )

                )

            [2] => Array
                (
                    [title] => Array
                        (
                            [@attributes] => Array
                                (
                                    [x] => 10
                                    [y] => 10
                                )

                        )

                    [author] => Array
                        (
                            [@attributes] => Array
                                (
                                    [x] => 20
                                    [y] => 20
                                )

                        )

                    [@attributes] => Array
                        (
                            [x] => 600
                            [y] => 400
                        )

                )

        )

    [@attributes] => Array
        (
            [x] => 600
            [y] => 400
        )

)


Here to code to achieve this:

<?php
// to get the xml from your desired location
$xmlStr = file_get_contents('books.xml');

// Call the function which converts xnl to an array
$xml = xmlstr_to_array($xmlStr);

// printing the array with formatting
echo "<pre>";
print_r($xml);
echo "</pre>";
?>

<?php
/**
 * convert xml string to php array - useful to get a serializable value
 *
 * @param string $xmlstr
 * @return array
 * @author Adrien aka Gaarf
 */
function xmlstr_to_array($xmlstr) {
  $doc = new DOMDocument();
  $doc->loadXML($xmlstr);
  return domnode_to_array($doc->documentElement);
}
function domnode_to_array($node) {
  $output = array();
  switch ($node->nodeType) {
   case XML_CDATA_SECTION_NODE:
   case XML_TEXT_NODE:
    $output = trim($node->textContent);
   break;
   case XML_ELEMENT_NODE:
    for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
     $child = $node->childNodes->item($i);
     $v = domnode_to_array($child);
     if(isset($child->tagName)) {
       $t = $child->tagName;
       if(!isset($output[$t])) {
        $output[$t] = array();
       }
       $output[$t][] = $v;
     }
     elseif($v) {
      $output = (string) $v;
     }
    }
    if(is_array($output)) {
     if($node->attributes->length) {
      $a = array();
      foreach($node->attributes as $attrName => $attrNode) {
       $a[$attrName] = (string) $attrNode->value;
      }
      $output['@attributes'] = $a;
     }
     foreach ($output as $t => $v) {
      if(is_array($v) && count($v)==1 && $t!='@attributes') {
       $output[$t] = $v[0];
      }
     }
    }
   break;
  }
  return $output;
}
?>
Function Source :  https://github.com/gaarf/XML-string-to-PHP-array

Hope this article helpful for you :)

Post a Comment

Analytics