0

i'v 1 multidimensional array from where i just want to print where (parent==0) by counting. It suppose to provide me 2 value but when i write return method on line-32 it show me d 1st one & whn write on line-35 , it show me the last one. how can i get all value from this loop when i return it ?

$menu=Array(
 Array(
            'id' => 73,
            'parent_id' => 0,
            'title' => 'MENU_73'
        ),

     Array
        (
            'id' => 74,
            'parent_id' => 73,
            'title' => 'MENU_73_74'
        ),

     Array
        (
            'id' => 75,
            'parent_id' => 74,
            'title' => 'MENU_73_74_75'
        ),

    Array
        (
            'id' => 76,
            'parent_id' => 0,
            'title' => 'MENU_73_74_76'
        ));

//Function to get Parents       
 $parent=0;
    function getMenu($menu, $parent){
    $list=array();
    foreach ($menu as $key=>$value){
        if($value['parent_id']==$parent){
            $list[count($value)]=$value;
            }
        }
    return $list; //line -35
    }
$listParent=getMenu($menu, $parent);
print_r($listParent);
3
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by rotten69
3

@doha786

It suppose to provide me 2 value but when i write return method on line-32 it show me d 1st one & whn write on line-35 , it show me the last one.

So when you ran the code it only show the first value and last value appear from the loop?

So you want to show all of the value?

You don't need this: $value['parent_id']

3

The snippet below should solve the problem you had with accessing the array $list [count($value)] which doesn't make sense to me. If you're trying to add the value of count to the array because your previous line stores the number of elements in an array to the index. The way you'd normally do it is $list [] .=count($value) the result you will get is this:
Array ( [0] => 3 [1] => 3 ) However, this won't tell you much about the data stored in the array.

Try this and let us know how you go.

$parent=0;
function getMenu($menu, $parent){

    $list=array();
    foreach ($menu as $key=>$value){

        if($value['parent_id']==$parent){

            $counter = count($value); // count the number of elements in an array
            $list[] = $value;
            echo $counter; 

            echo "<br>";
         }

     }

        return $list;

}

cheers;

0

@rotten69..really appreciate for ur reply. ya i got it..thanks a lot

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.