简体   繁体   中英

foreach loop for multidimensional array

I have the following array. how can I get the value of 'installed' key ie 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?

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

will output

1 1

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

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM