• Advertisement
Sign in to follow this  

[web] PHP sub-arrays not working(array in array)

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently whipping up a very simple CMS for my own site. To save me the hassle of passing around a database pointer to the showMenu function, or to querying for the menu layout in multiple places, I thought: I pass a $menu var to the showMenu function, and load the menu layout in a cms_loadMenu() function. This is my cms_loadMenu function:
function cms_loadMenu()
{
	$sql = "SELECT cms_pages.* , cms_menugroups.MG_Icon, cms_menugroups.MG_ID AS PID, cms_menugroups.MG_Name AS ParentName, cms_menugroups.MG_Order AS ParentOrder FROM cms_pages INNER JOIN cms_menugroups ON cms_pages.P_MG_ID = cms_menugroups.MG_ID WHERE cms_pages.P_Active ORDER  BY ParentOrder ASC, cms_pages.P_Order ASC";
	$res = mysql_query($sql) or die("MySQL Query error: " . $sql.mysql_error());

	$items = array();
	$menu = array();
	$parent = array();
	$parentID = -1;
	while ($row = mysql_fetch_array($res))
	{
		// New parent group
		if ($parentID != $row["P_MG_ID"])
		{		
			// Keep track of latest parent id			
			$parentID = $row["P_MG_ID"];
			
			// Create new arrays, and set default values;
			$items = array();
			$parent = array();
			$parent["name"] = $row["ParentName"];
			$parent["icon"] = $row["MG_Icon"];
			$parent["items"] = array();
			
			// Add the new parent group to the menu
			array_push($menu, $parent);
			$index++;
		}
		
		// New item
		$item = array();
		$item["id"] = $row["P_ID"];
		$item["name"] = $row["P_Name"];
		
		// Add the new item to the parent
		array_push($parent["items"], $item);
	}

	return $menu;
}


Now, the pain in the ass is this line: array_push($parent["items"], $item); When I do print_r of that function, it's empty: Array ( [0] => Array ( [name] => Algemeen [icon] => cat_main [items] => Array ( ) ) [1] => Array ( [name] => Diensten [icon] => cat_projects [items] => Array ( ) ) ) I have no idea what I'm doing wrong here. Any ideas? Toolmaker

Share this post


Link to post
Share on other sites
Advertisement
First off, using array_push() is a waste for one item. Use this syntax:

$my_array[] = $new_item;

It's both easier to read and faster to run. Now to your problem. PHP works with copies by default. You are pushing an element to the end of the array:

array_push($menu, $parent);

At that point, $menu contains a copy of parent. If you change $parent, only the local copy will change. $menu[$i]'s $parent will remain empty.

You are already using $index. You'll need to change:

array_push($parent["items"], $item);

to

$menu[$index - 1]['items'][] = $item;

That is very ugly, so I'd recommend that you rethink it a bit to make it more clean.

Share this post


Link to post
Share on other sites
I already solved it, by make use of classes. I made a MenuGroup and MenuItem. I have a list of MenuGroup's which in turn have an array of Items. Works very good, and is extremely good to read.

But thanks for the answer :)

Toolmaker

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement