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);
Member Avatar for LastMitch

@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']

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;

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

You're welcome,buddy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.