Debendra Samal
Debendra Samal

Reputation: 71

foreach loop for multidimensional array

I have the following array. how can I get the value of 'installed' key i.e 1. which value I have to check in my application.

Array
(
    [0] => Array
        (
            [id] => 53686899
        )

    [1] => Array
        (
        [installed] => 1
            [id] => 542813519
        )

    [2] => Array
        (
        [installed] => 1
            [id] => 567790764
        )
     [3] => Array
        (

            [id] => 567570764
        )
)

using foreach loop how can i do this job? anybody can plz help me?

Upvotes: 1

Views: 194

Answers (4)

Prasanth Bendra
Prasanth Bendra

Reputation: 32810

Try this :

foreach ($array as $value){
   if(array_key_exists('installed',$value)){
      echo $value['installed']. "<br />";
   }
}

If you are not checking for array_key_exists it will show error in first loop.

Upvotes: 0

Grant Timmerman
Grant Timmerman

Reputation: 404

Loop through the array and get the 'installed' key's value:

foreach ($array as $innerArray) {
    echo $innerArray['installed'];
}

Upvotes: 0

user74847
user74847

Reputation: 361

foreach ($array as $value)
{
   echo $value['installed']. "<br />";
}

will output

1 1

Upvotes: 1

zerkms
zerkms

Reputation: 255095

Absolutely the same way like when you iterate 1 dimensional array:

foreach ($array as $value) {
    var_dump($value);
    var_dump($value['installed'];
}

Upvotes: 0

Related Questions