Hello everyone this is my first post but I just couldn't figure this out normally I can troubleshoot until it at least half ass works, but since I was'nt able to find information I am hoping ya'll can help.
Anyways I have a class that constructs a menu from an XML file.
XML FILE
<?xml version="1.0" encoding="iso-8859-1"?>
<menus>
<menu name="header" position="top" style="0">
<category name="schools">
<item url="#" tag="id::section::class::hi">home</item>
<item url="#" tag="id::two">high school</item>
<item url="#" tag="id::three">junior high</item>
<item url="#" tag="id::four">intermediate school</item>
<item url="#" tag="id::five">elementary school</item>
</category>
</menu>
<menu name="main" position="right" style="0">
<category name="Administration">
<item url="#" tag="lang::en">Contact</item>
<item url="#" tag="lang::en">Webmaster</item>
</category>
<category name="Test">
<item url="#" tag="lang::en">Test1</item>
<item url="#" tag="lang::en">Test2</item>
</category>
</menu>
</menus>
My class is as follows
<pre>
<?php
class menu {
private $xml;
private $position;
private $style;
private $categories;
function __construct($id) {
$this->xml = @simplexml_load_file('menus.xml') or die('Invalid file!');
$this->getMenu($id);
}
function getMenu($id) {
$menu = $this->xml->xpath("//menu [@name='$id']");
$this->position = $menu[0]['position'];
$this->style = $menu[0]['style'];
$this->getCategory($menu[0]);
}
function getCategory($menu) {
$index = 0;
foreach ($menu->category as $category) {
$this->categories[] = $category['name'];
foreach ($category->item as $item) {
$this->categories[$index][] = $item;
$this->categories[$index][] = $item['url'];
}
$index++;
}
$this->displayArray($this->categories);
}
function displayArray($array) {
print_r($array);
}
}
//Test Class
$menu = new menu("main");
?>
</pre>
Ok my problem, I don't know if this is me just bad with arrays or what ... I want to loop through all the categories and store them in the class $categories array, so categories I am guessing is going to be a three dimensional array when I am done. but when I come to line 27 I start to get errors (Cannot create unnamed attribute) :queasy: . So I tried
$this->categories[$index][$index][] = $item;
But this produces error (Objects used as arrays in post/pre increment/decrement must return values by reference)
So is this jsut me being bad with arrays or the way the array is formed by simplexml
Output when edit out line 27, 28
Array
(
[0] => SimpleXMLElement Object
(
[0] => Administration
)
[1] => SimpleXMLElement Object
(
[0] => Test
)
)
My goal would to store this items in an easy accesible array (pref. associative) but I am really having trouble going about it any help would be oh so great!
Thank you!