Looping array problems.

I have 2 groups of items, items (which are not always parent items) and child items (which should only appear in association with the corresponding parent item from the main item stack), and both are inside an array loop for layers. I’ve gotten the child items correctly associated with the parent items they belong to and the main item group associated with their layers correctly, but because of the way the loops are set up, the child items are not associating with their own correct layers (and instead taking on the values of the parent item’s layers). The child items need to stay in the loops they’re already in for functionality later though… I put another array outside the item loop to associate the child items directly to the corresponding layers, but I’m too brain-dead to finish connecting the dots so that they child items will associate with their layers properly. I’ve put a condensed version of the problem codes below (pseudo code for simplicity and hopefully clarity as well). Can anyone help?
[php] $layers = array();
//SQL query to get layers.
while loop results{
$layer_id = $row[‘layer_id’];
//more layer data.

				$layers[$layer_id] = array(
					'children'	=> array(),
					'items' 	=> array(), // To be filled in later (assoc)
				);
			}

			// Get all the possible items (including child items)
			SQL QUERY

			while loop results
			{		
			$parent = isset($row['parent_item']) ? $row['parent_item'] : 0; //parent items come back with the row.
			$shop = $row['shop']; //this will only be different for child items later.

				$layer_id = $row['item_layer'];
				$item_id = $row['item'];
				$children = array();

					$layers[$layer_id]['items'][$item_id] = array(
					'children'	=> array(),
					'shop'	=> $shop,
					'parent'	=> $parent, 
					);

				if ($shop == 1)
				{
				$layers[$layer_id]['children'][$item_id] = array(
					'children'	=> array(),
					'parent'	=> $parent, 
					);
				}

				if ($layers[$layer_id]['items'][$item_id]['parent'] > 0)
				{
					//SQL query to get child items where the item is child to a parent item. 
					while loop results
					{
					
						$clayer_id = $child_row['item_layer'];
						$citem_id = $child_row['item'];
		
						$layers[$layer_id]['items'][$item_id]['children'][$citem_id] = array(
							'layer'		=> $clayer_id,
							'item'		=> $citem_id,
							'parent'	=> $cparent, 
						);
					}
				}							
			}

			//SQL query to fetch current items. This is the value that later needs to be correctly associated with the right layer (even inside the child loop).	
			while loop results
			{
				// Update the $layers array - current item
				// If there is no data in the dynamo users table, set to 0
				$layer_id = $row['dynamo_user_layer'];
				$cur_id = $row['dynamo_user_item'];

				if (isset($layers[$layer_id]))
				{
				
					$layers[$layer_id]['current'] = $cur_id;
				}
			}
				
			// Create the template loops and stuff by looping through $layers
			foreach ($layers as $layer_id => $layer_data)
			{
				$current_item = $layer_data['current'];
				$item_exists = isset($layer_data['items'][$current_item]) && $current_item > 0;
				$true_item = ($item_exists) ? $current_item : $default;

				$template->assign_block_vars('layer', array(
					'ID'			=> $layer_id,
					'TRUE_ITEM'		=> $true_item,
					));
				//These values will echo correctly for all items, but because of the way its looped (see below), they won't come up with the appropriate child items. 

                                   //Tried to put another loop here for child items only. Still wouldn't associate child items with correct layers though since the html child calls are still inside the items loop (and need to stay there for the html to show properly) 

				// Now loop through the items in this layer
				foreach ($layer_data['items'] as $item_id => $item_data)
				{
				
											
							$template->assign_block_vars('layer.item', array(
								'SHOP'	=> isset($item_data['shop']) ? $item_data['shop'] : 0,
								'PARENT'	=> isset($item_data['parent']) ? $item_data['parent'] : 0,
								'ID'		=> $item_id,
							));
							
							
							
							if (isset($item_data['children'][$citem_id]))
							{

								foreach($item_data['children'] as $citem_id => $child_data)
								{
									$template->assign_block_vars('layer.item.child', array(
										'LAYER'		=> $child_data['layer'],
										'PARENT'	=> isset($child_data['parent']) ? $child_data['parent'] : 0,
										'ID'		=> $child_data['item'],
									));
								}

							}
						}
					}
					
				}
								
			}
			}[/php]

Html below

[code]

					<input type="radio" id="layer-{layer.ID}-{layer.item.ID}-radio" name="layer-{layer.ID}" value="{layer.item.ID}" <!-- IF layer.item.ID == layer.TRUE_ITEM --> checked="checked"<!-- ENDIF --> /></label> 

<input type=“radio” id=“layer-{layer.item.child.LAYER}-{layer.item.child.ID}-radio” name=“layer-{layer.item.child.LAYER}” value="{layer.item.child.ID}" checked=“checked” />

[/code]
Sponsor our Newsletter | Privacy Policy | Terms of Service