# XML nested array

**URL:** <https://forum.kirupa.com/t/xml-nested-array/235913>\
**Category:** programming\
**Created:** [August 15, 2007, 6:57pm UTC](https://forum.kirupa.com/t/xml-nested-array/235913 "2007-08-15T18:57:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_morton](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mr\_morton](https://forum.kirupa.com/u/mr_morton)\
**Post date:** [August 15, 2007, 6:57pm UTC](https://forum.kirupa.com/t/xml-nested-array/235913/1 "2007-08-15T18:57:49Z")

</div>

Hi good people!

First of all, i’m not very experienced with PHP or XML, so this may be a very simple/basic problem! But I just can’t work out why this do not work.

I have a XML based menu (with submenues) that I need to save to mysql via PHP, I found  
a XMLParser class [here](http://eric.pollmann.net/items/2007/2/2007_02_12_xmlparser/XMLParser.php) that can make a nested array.

Then I want to run through via PHP and a recursive function. But for some reason I can’t make it work with the multiple sub levels that I want in my menu.

Here is the code:

```php
<?php
require_once('../../../inc/XMLParser.php');

$menuXml='<?xml version="1.0" encoding="UTF-8" ?>
<root id="menu">
<menu id="14" label="PAGE 1" locked="true">
  <menu id="224" label="PAGE 1 1"></menu>
  <menu id="133" label="PAGE 1 2"></menu>
  <menu id="411" label="PAGE 1 3"></menu>
  <menu id="7" label="PAGE 1 4">
    <menu id="285" label="PAGE 1 4 1"></menu>
  </menu>
  <menu id="1" label="PAGE 1 5"></menu>
  <menu id="2" label="PAGE 1 6"></menu>
  <menu id="10" label="PAGE 1 7"></menu>
  <menu id="5" label="PAGE 1 8"></menu>
</menu>
<menu id="11" label="PAGE 2" locked="true">
  <menu id="20" label="PAGE 2 1">
      <menu id="202" label="PAGE 2 1 1">
        <menu id="202" label="PAGE 2 1 1 2"></menu>
    </menu>
    <menu id="201" label="PAGE 2 1 2"></menu>
  </menu>
  <menu id="19" label="PAGE 2 2"></menu>
</menu>
</root>';

$parser = new XMLParser($menuXml, 'raw', 1);
$tree = $parser->getTree();

$level=0;
function doTheArray($array){
        for ($i=0; $i<count($array);$i++){
            $id=$array[$i]["ATTRIBUTES"]["ID"];
            $label=$array[$i]["ATTRIBUTES"]["LABEL"];
            echo($label."<br />");
            
            $curArray=$array[$i]["MENU"];
            if (count($curArray)>0){
                $level++;
                doTheArray($curArray);
                $level--;
            }
        }
    }
doTheArray($tree["ROOT"]["MENU"]);

?>

```

It has problems with the sublevel 2 and higher where it some how ignores them, I cant se why, can somebody in here see the problem?
