need vector-like functionality

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!

Moderator: General Moderators

Post Reply
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

need vector-like functionality

Post by konstandinos »

feyd | Please use

Code: Select all

,

Code: Select all

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
i hope the helps clarify things.

thanks


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Looks like a foreach with some isset() and array_key_exists() calls.
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

damn that was fast!

and i was just busy reading through your sticky post on code pasting too...

i am newbie here, so pardon. thanks for your help too.
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The array type in PHP natively supports dynamically adding information to them, numeric or named (keyed), as well as altering existing ones.

http://php.net/language.types.array
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

i am glad that it CAN be done. that's pretty much all i needed to know.

and thanks for pointing me in the right direction. good link.

cheerio
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

ok here is the php code:

Code: Select all

$base = array();
$items = mysql_query("select * from new_bikes");

while ($item = mysql_fetch_row($items))
{
	$item_category = $item[0];
	$item_brand = $item[1];
	$item_name = $item[2];

	if(!array_key_exists($item_category, $base))
	{
		$base[$item_category] = array();
	}
	
	if(!array_key_exists($item_brand, $base[$item_category]))
		$base[$item_category][$item_brand] = array();
		
	array_push($base[$item_category][$item_brand], $item_name);
}

foreach (array_keys($base) as $category)
{
	echo "Category: " . $category . "<br/>";
	
	foreach (array_keys($base[$category]) as $brand)
	{
		echo "Brand: " . $brand . "<br/>";
		
		foreach (array_values($base[$category][$brand]) as $item)
		{			
			echo "Name: " . $item . "<br/>";
		}
	}
}
it works like a bomb. thanks for your help feyd.
Post Reply