0
  Array
       (
    [30514] => Array
         (
        [1001] => Array
            (
                [Marks_M] => 89
                [Marks_C] => 87
            )

       )
   )

This is my multidimensional array.How do i print value of Marks_M using foreach loop.

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
qwerty
  • 17
  • 3

4 Answers4

1

You can do it this way

echo $outerarray['30514']['1001']['Marks_M'];

$outerarray['30514'] will get you the second Array inside.

echo $outerarray['30514']['1001'] will get you the third Array inside.

Once you get the third one, you can get the value you want using its key eg: ['Marks_M']

jonju
  • 2,711
  • 1
  • 13
  • 19
0

you can loop an array to reach to the last array in your list and can get the value of Marks_M

    <?php

    $arr = array(
        30514=>array(
            1001=>array(
                'Marks_M'=>89,
                'Marks_C'=>87
                )
            )
        );

    foreach ($arr as $value) {
        foreach ($value as $val) {
            if(array_key_exists('Marks_M',$val))
            {
                 echo $val['Marks_M'];
            }
        }

    }

    ?>
Mittul Chauhan
  • 417
  • 1
  • 6
  • 18
  • Thanks you are right. But i dont want to take foreach for $value array i need directly value of both Marks_C and Marks_M since i have one more loop before that – qwerty Aug 10 '16 at 06:30
  • @qwerty at times when we use forloops, we dont need to remember key names like 30514 , 1001 etc .. so evenif these keys changes, we will get our value inside it .. – Mittul Chauhan Aug 10 '16 at 06:32
  • @qwerty however, glad you have resolved your issue or got quick solution for this .. cheers !!! – Mittul Chauhan Aug 10 '16 at 06:33
  • I need to insert each values to database. Im struggling with that – qwerty Aug 10 '16 at 06:33
  • ya 30514 is a key n it changes accordingly. I need value for that particular key the value of marks – qwerty Aug 10 '16 at 06:35
  • then u should go with forloop dear .. so u dont need to worry about if any key gets changed coz u r always gonna go inside that loop – Mittul Chauhan Aug 10 '16 at 06:37
0

You can simply do this:

$arr = array('30514'=>array('1001'=>array('Marks_M'=>89,'Marks_C'=>87)));

echo $arr[30514][1001]['Marks_M'];

and if you want to loop then you can use this:

foreach($arr as $array){
    foreach($array as $key=>$value){
        echo $value['Marks_M'];
    }   

    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
0

You can do something like this

<?php
// consider you have multidimentional array and second level sub array have Marks_M index
$arr = array(
    30514=>array(
        1001=>array(
            'Marks_M'=>89,
            'Marks_C'=>87
            )
        )
    );

foreach ($arr as $sub_arr) {
    foreach ($sub_arras $subjects) {
       if(array_key_exists('Marks_M',$subjects))
       {
           echo $subjects['Marks_M'];
       }
    }

}

?>
Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42