searching within arrayv - multidimensional needed?...
Posted: Mon Dec 20, 2004 1:08 pm
Hi
I wonder if you guys can help me with this.
I'm setting up a heirarchical menu system on my site.
I have an array...
So basically I've got my array set up so that there are two values stored in each array value, by using a delimeter || between each.
The first value (before the delimeter) is what type of menu item it is.
There are 3 types... cat/item/sub and they have the following heirarchy...
Now as this is a heirarchical menu I need it so that subs are only shown if anything under their parent 'item' is clicked.
So when I'm looping through I'm doing a check on each row in the array.
Firstly to see what menutype the item is (cat/item/sub)
Then if it's item/sub I'm doing this...
This is not quite what I need to do. I realise now that I need to see what the next values in the array are whilst I'm looping through it.
Obviously the child rows with $menuitem = 'sub' are only shown when either...
a) the parent item is selected
b) another child 'sub' is selected
And only one item's subs can be shown at a time.
So whether the sub is shown depends where you are in the heirarchy whilst looping through - and the only way to find that out is to be able to access values in the array whilst you are looping through.
I think this method I've got is getting far too complicated and there must be a better way. I thought of setting up a multi-dimensional array but I didn't know how to work it with what I'm trying to achieve.
I'm hoping that my explanation is clear enough for someone to understand what it is I'm trying to get going here.
Any ideas?
Thanks in advance guys and gals.
Ben
I wonder if you guys can help me with this.
I'm setting up a heirarchical menu system on my site.
I have an array...
Code: Select all
$menu['News'] = 'item||http://www.dealer-world.com/news.php';
$menu['Archive'] = 'item||archive.php';
$menu['Publications'] = 'cat||';
$menu['AMD'] = 'item||testamd.php';
$menu['Editorial Policy'] = 'sub||test.php';
$menu['Advertising'] = 'sub||test2.php';
$menu['Schedule'] = 'sub||test3.php';
$menu['AMD Directory'] = 'item||amddirectory.php';
$menu['AMD Pro-Guides'] = 'item||amdproguides.php';
$menu['AMD Builder Book'] = 'item||amdbuilderbook.php';
// PARSE EACH ARRAY ITEM INTO $menutext AND $menuvalue
foreach ($menu as $menutext => $menuvalue) {
// EXPLODE EACH $menuvalue INTO ANOTHER ARRAY DIMENSION BASED ON THE || DOUBLE PIPE DELIMETER
$menuvaluearray = explode("||", $menuvalue);
$menutype = $menuvaluearray['0'];
$menuurl = $menuvaluearray['1'];The first value (before the delimeter) is what type of menu item it is.
There are 3 types... cat/item/sub and they have the following heirarchy...
Code: Select all
cat
--item
--item
--item
cat
--item
--item
----sub
----sub
----sub
--item
--item
----sub
----subSo when I'm looping through I'm doing a check on each row in the array.
Firstly to see what menutype the item is (cat/item/sub)
Then if it's item/sub I'm doing this...
Code: Select all
if (strstr($_SERVER["SCRIPT_URI"], $menuurl)) {
// CURRENT URL MATCHES $menuurl
} else {
// NO MATCH
};Obviously the child rows with $menuitem = 'sub' are only shown when either...
a) the parent item is selected
b) another child 'sub' is selected
And only one item's subs can be shown at a time.
So whether the sub is shown depends where you are in the heirarchy whilst looping through - and the only way to find that out is to be able to access values in the array whilst you are looping through.
I think this method I've got is getting far too complicated and there must be a better way. I thought of setting up a multi-dimensional array but I didn't know how to work it with what I'm trying to achieve.
I'm hoping that my explanation is clear enough for someone to understand what it is I'm trying to get going here.
Any ideas?
Thanks in advance guys and gals.
Ben