简体   繁体   中英

Associative Multi-Dimensional PHP Array Loop

I am new to Multi-Dimensional arrays and working on building my php skills as well. I have an associative array which I want to get the nested elements of the array but not have to worry about the multi-dem arrays name. Example:

Array ( 
[cur_wea_array] => Array ( [status] => current [day] => 0 ) 
[for_wea_array0] => Array ( [status] => current_forecast [day] => 1 ) 
[for_wea_array1] => Array ( [status] => current_forecast [day] => 2 )
[for_wea_array3] => Array ( [status] => current_forecast [day] => 3 ) )

I would like to not to have to worry about the cur_wea_array element and just loop through and get the status element. Is there a way you can loop [%wildcard][day] or something? So I can get all the status or day elements without having to specify [cur_wea_array] and [for_wea_array0]?

foreach ( $array as $inner_array )
{
    echo $inner_array['day']; // or $inner_array['status'];
}

This is a basic foreach example specific to your case, every time the loop iterates, $inner_array is populated with each inner array

You can just use a foreach loop :

foreach ($array as $array_element) 
{
    // your $array_element contains the inner array
}

If you only want the array keys, you can access them with something like:

$array_keys = array_keys($array);

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