Jump to content

Need help abut understanding array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
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


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...

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
1. print_r is used to display information in a human readable form. It is not usually used to display data in production to the user, but more often used by developers for debugging information. Instead of using print_r, you can echo the individual indices of the array:

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

You'll need to learn more about arrays to use them effectively. PHP: Arrays - Manual

2. This is assigning an array to variable which also happens to be an array:
$categories[$id] = array('name' => $name, 'children' => array());

3. .= is concatenation. It is often used in strings to combine two parts.
$myString = "this is ";
$myString .= "my string.";
echo $myString;

This will print "this is my string."

#3
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Thanks Jordan,by the way.I tried


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

echo $categories[12]['children']['id'];  


it gives me
Notice: Undefined index: id in D:\xampp\htdocs\plaincart\admin\library\functions.php on line 111

Are there any wrongs with it?Thanks..

#4
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
are you sure $parentId==12?
Its the only logical solution I can see...

#5
Pa28

Pa28

    Newbie

  • Members
  • Pip
  • 4 posts

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

echo $categories[12]['children']['id'];  


The '[]' syntax on your first line creates a new numerical array index. What that's saying, with each iteration of the loop, is

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

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

....

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


So to access the individual elements, you need to do:

echo $categories[12]['children']['id'][0];

(obviously replacing '0' with the number of the element that you want).

#6
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Thanks pa28..