Topic: Need help abut understanding array

Hi all,i found out a tutorial about ecommerce on phpwebcommerce.com..There are a some codes like this

	$sql = "SELECT cat_id, cat_parent_id, cat_name
			FROM tbl_category
			ORDER BY cat_id";
	$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());
	
	$categories = array();
	while($row = dbFetchArray($result)) {
		list($id, $parentId, $name) = $row;
		
		if ($parentId == 0) {
			// we create a new array for each top level categories
			$categories[$id] = array('name' => $name, 'children' => array());
		//print_r($categories);
		} else {
			// the child categories are put int the parent category's array
			$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);	
		//print_r($categories);
		}
	}	
	
	// build combo box options
	$list = '';
	foreach ($categories as $key => $value) {
		$name     = $value['name'];
		$children = $value['children'];
		
		$list .= "<optgroup label=\"$name\">"; 
		
		foreach ($children as $child) {
			$list .= "<option value=\"{$child['id']}\"";
			if ($child['id'] == $catId) {
				$list.= " selected";
			}
			
			$list .= ">{$child['name']}</option>\r\n";
		}
		
		$list .= "</optgroup>";
	}
	
	return $list;

The database like this

cat_id   cat_parent_id         cat_name 	                   cat_description 	                           cat_image
17 	    13 	            Hunter X Hunter     Story about hunter and combat 	 
12         0 	                    Cars 	                Expensive and luxurious cars 	dce08605333d805106217aaab7f93b95.jpg
13 	    0 	                    Manga 	                It's all about manga, yay.... 	2a5d7eb60c1625144b3bd785bf70342c.jpg
14 	    12 	            Volvo 	                Swedish luxury car 	 
15 	    12 	            Mercedes-Benz 	Expensive but real good 	 
16        13 	            Naruto 	                This is the story of Naruto and all his gang 	 
18 	    0 	                    testing 123 	        tes

When i try to print_r like this

$categories[$id] = array('name' => $name, 'children' => array());
print_r($categories);

it gives me

Array ( [12] => Array ( [name] => Cars [children] => Array ( ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( ) ) [13] => Array ( [name] => Manga [children] => Array ( ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) [1] => Array ( [id] => 15 [name] => Mercedes-Benz ) ) ) [13] => Array ( [name] => Manga [children] => Array ( [0] => Array ( [id] => 16 [name] => Naruto ) [1] => Array ( [id] => 17 [name] => Hunter X Hunter ) ) ) [18] => Array ( [name] => testing 123 [children] => Array ( ) ) ) 

and

$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);	
print_r($categories);

it gives me

Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) ) ) [13] => Array ( [name] => Manga [children] => Array ( ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) [1] => Array ( [id] => 15 [name] => Mercedes-Benz ) ) ) [13] => Array ( [name] => Manga [children] => Array ( ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) [1] => Array ( [id] => 15 [name] => Mercedes-Benz ) ) ) [13] => Array ( [name] => Manga [children] => Array ( [0] => Array ( [id] => 16 [name] => Naruto ) ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) [1] => Array ( [id] => 15 [name] => Mercedes-Benz ) ) ) [13] => Array ( [name] => Manga [children] => Array ( [0] => Array ( [id] => 16 [name] => Naruto ) [1] => Array ( [id] => 17 [name] => Hunter X Hunter ) ) ) ) 

1.How should i format the output so i can learn the results that was returned?
2.What does
   

$categories[$id] = array('name' => $name, 'children' => array());

     and
   

$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);

means?
3.What is the uses of .= ?

Thanks a lot...God bless you all.. ^_^

Last edited by Handoyo (2009-09-30 02:08:40)

Re: Need help abut understanding array

1)
If you do a print_r($categories) just before the line "// build combo box options" you will see the full array that was built from the DB.

The print_r statements you are referring to happen on each row of the database so it's hard to understand what each one is doing.

2)
that is the code that is building the categories array. Basically it is putting all your results into a hierarchical array. Once you do the print_r from where I said in 1) above you will see. Also, when you do a print_r be sure that you View Source so that you can see all the indentions that show you what is a child of the parent.

3) .= is a concanation operator. It's a shorcut so that instead of using:

$var = $var . "a new string";

you can just do:

$var .= "a new string";
I dream with an XML intereface

Re: Need help abut understanding array

Thanks  tank..By the way how should i format the print_r so i can read it...God bless you..

Last edited by Handoyo (2009-10-01 20:09:00)

Re: Need help abut understanding array

Handoyo:

You can do something like this:

echo "<pre>";
print_r($categories);
echo "</pre>";

That should help to make it a little more readable.

Hope that helps.

Exercise your faith!

Re: Need help abut understanding array

Ok Yannick,i'll try it..Thanks a lot..