Skip to content
Advertisement

i run this code but it donot type the array

  $studentArray =  array(
    array("ahmed2",15,3.5),
    array("ahmed1",15,2.4),
    array("ahmed3",29,3.9),
    array("ahmed4",22,3),
    array("ahmed5",23,2.8)
  );
  foreach($studentArray as $key => $value ){
        echo $key. '-'.$value.'<br>'; 
  }

Advertisement

Answer

Since you have student detail array in $studentArray, you need an extra foreach to loop through inner array.

Try below code in your structure.

    $studentArray =  array(
        array("ahmed2",15,3.5),
        array("ahmed1",15,2.4),
        array("ahmed3",29,3.9),
        array("ahmed4",22,3),
        array("ahmed5",23,2.8)
    );
    foreach($studentArray as $studentDetail ){
        foreach($studentDetail as $key => $value ){
            echo $key. '-'.$value.'<br>';
        }
    }

For more better understanding, have a try with below code.

    $studentArray =  array(
        array("val1"=>"ahmed2","val2"=>15,"val3"=>3.5),
        array("val1"=>"ahmed1","val2"=>15,"val3"=>2.4),
        array("val1"=>"ahmed3","val2"=>29,"val3"=>3.9),
        array("val1"=>"ahmed4","val2"=>22,"val3"=>3),
        array("val1"=>"ahmed5","val2"=>23,"val3"=>2.8)
    );
    
    foreach($studentArray as $student){
        foreach($student as $key => $value) { 
            echo $key. '-'.$value.'<br>';
        }
        echo '<br>';
    }
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement