ahmad talat ahmad talat - 1 month ago 6
PHP Question

Multidimensional array echo

Array
(
[mohammad] => Array
(
[physics] => 35
[maths] => 30
[chemistry] => 39
)

[Qadir] => Array
(
[phyiscs] => 30
[maths] => 32
[chemistry] => 29
)

[zara] => Array
(
[physics] => 31
[maths] => 22
[chemistry] => 39
)

)


I am trying to echo this multi dimensional array.

It works fine with print_r but I want to echo it and display the output as
mohammad has 35 marks in physics and so on with every value.

foreach ($marks as $key => $val) {
echo $val;
}


I know I have to pass every value indiviually but I dont know the syntax for it. Thanks
Please let me know how to correctly do this.

Answer

I believe this does what you wanted... feel free to modify appearance to your tastes :)

$marks = array(
  'mohammad' => Array
  (
    'physics' => 35,
    'maths' => 30,
    'chemistry' => 39
  ),

  'Qadir' => Array
  (
    'physics' => 30,
    'maths' => 32,
    'chemistry' => 29
  ),

  'zara' => Array
  (
    'physics' => 31,
    'maths' => 22,
    'chemistry' => 39
  )
);

foreach ($marks as $key1 => $person) {
  foreach ($person as $subject => $val2) {
    echo ' '. $key1 .' has '. $person[$subject] .'marks in '. $subject .'<br>';
  }
}

//Array info:
//http://php.net/manual/en/language.types.array.php
Comments