array ...

Posted on 16th Feb 2014 by admin

hi
Code: function formatCategories($categories, $parentId)
{
// $navCat stores all children categories
// of $parentId
$navCat = array();

// expand only the categories with the same parent id
// all other remain compact
$ids = array();
foreach ($categories as $category) {
if ($category['cat_parent_id'] == $parentId) {
$navCat[] = $category;

}


// save the ids for later use
$ids[$category['cat_id']] = $category;

}


print_r($ids[$category[0]]);

If i am printing the array using,
print_r($ids[$category[0]]);here 0 is the start key of the array(as shown in last line of codes)..OR print_r($ids[$category[12]]);
here 12 is the cat_id of a product...

I am getting notice as,Notice: Undefined offset: 0.
without out the display of expected output..

But if i use print_r($ids);
i get,

Array ( [12] => Array ( [cat_id] => 12 [cat_parent_id] => 0 [cat_name] => Cars [cat_image] => dce08605333d805106217aaab7f93b95.jpg [cat_description] => Expensive and luxurious cars ) [13] => Array ( [cat_id] => 13 [cat_parent_id] => 0 .................................etc....

And
if i use print_r($ids[$category['cat_id']]);after for each loop
i get,
Array ( [cat_id] => 17 [cat_parent_id] => 13 [cat_name] => Hunter X Hunter [cat_image] => 746e05a7a629d53bc488115a75ee35d9.jpg [cat_description] => Story about hunter and combat )

The question is why do i get notice of undefined offset after using those,print_r($ids[$category[0]]);(as shown in last line of codes)..OR print_r($ids[$category[12]]); statements instead of expected output..


could you explain this please..

thanks

Other forums