how to loop through different level of multidimensional array in php using foreach loop

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>";
            }
            ?>