PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
hi all
i need to generate a 3-tier menu system dynamically and thus want something like a 3d vector. but i cannot find any vector functions in php, only arrays.
but from what i understand about theory of programming, 3d arrays cannot be dynamic. well... i bet they can but i need something simple like myVector.pushBack(item) or even myVector.pushBack(anotherVector) etc.
i am pulling a list of items from a database. each item has a name, a category, a sub-category, and a type.
in order to generate the 3 tier menu i need to be able to dynamically alter the 3d vector as i iterate through each item. know what i mean?
how do i implement 3d vectors in php? or more simply, how do i implement a vector that can be dynamically altered?
i have code to do exactly what i want in python. now i am trying to do the same in php.
the python code:
[syntax="python"]
base = {}
for item in items:
if not base.has_key(item.category):
base[item.category] = {}
if not base[item.category].has_key(item.type)
base[item.category][item.type] = {}
if not base[item.category][item.type].has_key(item.brand)
base[item.category][item.type][item.brand] = []
base[item.category][item.type][item.brand].append(item.name)
for cat in base.keys():
print cat
for typ in base[cat].keys():
print typ
for brand in base[cat][typ].keys():
print brand
for item in base[cat][typ][brand]:
print item
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
ok from what i've read, isset() and array_key_exists() are quite similar. the difference being that the former returns false if the key is assigned to null.
either way, once ive used either of the above to check for a key's existence, and assuming it doesn't exist, then how would i go about appending a new key or even another array to the current array?
please bear in mind that i might append another array, which will in future be populated with additional key-value pairs.
it's the dynamic adding of data and/or arrays to a current array that i am after. array_push doesn't seem to do the trick although i'm still wrapping my mind around that function.
so based on what i'm trying to do (see python code), i can use isset() and/or array_key_exists() for one aspect of the problem, but for inserting new arrays within arrays and/or inserting variables into already existing arrays dynamically, what should i be looking at?