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 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
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?