-1

there is an inner array at the 0 index of very first array. how do I loop through this array by using foreach loop? only ali has one more array.

            <?php
            $marks=[
                "ali" => ["physics" => 55, "chemistry" => array(12, "practical"=>45),"math"=>18],
                "salman" => ["physics" => 34, "chemistry"=>44,"math"=>68],
                "Mohan" => ["physics" => 98, "chemistry"=>40,"math"=>89]
            ];
            foreach($marks as $key => $val)
            {
                echo "$key ";
                    foreach($val as $val2) 
                            {
                                echo " $val2 ";
                                // foreach($val2 as $InVal){
                                //     echo $InVal;
                                // }
                            }
                            echo "<br>";
            }
            ?>
shine
  • 13
  • 3

1 Answers1

0

I don't know what problem you are facing but for the mentioned array you can get the value by this:

    <?php
$marks=[
    "ali" => ["physics" => 55, "chemistry" => array(12, "practical"=>45),"math"=>18],
    "salman" => ["physics" => 34, "chemistry"=>44,"math"=>68],
    "Mohan" => ["physics" => 98, "chemistry"=>40,"math"=>89]
];
foreach($marks as $key => $val){
    foreach($val as $subKey => $subval){
        if(is_array($subval)){
            foreach($subval as $childKey => $childVal){
                echo $childVal;
                echo "<br>";
            }
        }else{
            echo $subval;
            echo "<br>";
        }
    }
}