How to start a foreach loop in array?

I have one array has 3 array, the 3 inner array to be displayed by the cycle, but I can't run the loop inside of the main body. Please tell me how to do it correctly?
$adresses = array(

'Kazan' => array(
'name' => 'Kazan',
'header_phone' => '123',
'header_adress' => '123',
'header_adress2' => '123',
),

'belgorod' => array(
'name' => 'Belgorod',
'header_phone' => '123',
'header_adress' => '123',
'header_adress2' => '123',
),

'volgograd' => array(
'name' => 'Title города_3',
'header_phone' => '123',
'header_adress' => '123',
'header_adress2' => '123',
),

);

I'm trying to do so, but it's worse, so the cycle does not start:
$adresses = array(
foreach($rows as $row)
{
'Kazan' => array(
'name' => 'Kazan',
'header_phone' => '123',
'header_adress' => '123',
'header_adress2' => '123',
),
}
);
April 3rd 20 at 17:35
2 answers
April 3rd 20 at 17:37
foreach($adresses as $address => $details){
 echo "$address<br>";
 foreach ($details as $key => $value ){
 echo "$key: $value<br>";
}
 echo "<hr>";
}
Forgot to clarify, I need to get data from arrays in a loop in the bulk adresses . The output is not necessary - Lilyan.Abbott commented on April 3rd 20 at 17:40
@Lilyan.Abbott, write the array which you want to what he looks like. Because it is not clear what you want. - Bryce.Konopelski commented on April 3rd 20 at 17:43
@Bryce.Konopelski,
$adresses = array(

while ( have_rows( 'contacts', 'option' ) ) : the_row();
get_sub_field('city-name eng') => array(
'name' => get_sub_field('city-name-rus'),
'header_phone' => get_sub_field('number'),
'header_adress' => get_sub_field('adres'),
'header_adress2' => get_sub_field('adres2'),
),
endwhile;

);


get_sub_field - getting values from fields in the admin area. That is, I want the loop to fill the array of keys of the array $adresses - Lilyan.Abbott commented on April 3rd 20 at 17:46
April 3rd 20 at 17:39
Write about foreach, but using a while loop)
foreach($adresses as $key => $val) {
 foreach($val as $name => $phone){
 //actions $name or $phone
}
}
Thank you! Can you please on my example to show how to do it? I can not navigate (

$adresses = array(
while ( have_rows( 'contacts', 'option' ) ) : the_row();
'Kazan' => array(
'name' => 'Kazan',
'header_phone' => the_sub_field( 'number' );,
'header_adress' => the_sub_field( 'adr' );,
'header_adress2' => the_sub_field( 'adr2' );,
),
endwhile;
);

So I'm trying to do. I need to get data from arrays in a loop in the bulk adresses . - Lilyan.Abbott commented on April 3rd 20 at 17:42
So I like your example and showed the only conclusion to add
foreach($adresses as $key => $val) {
 foreach($val as $name => $phone){
 //actions $name or $rope
 echo 'phone Number: '.$phone;
}
}


or I just don't understand what you want) - Rachel_Lang commented on April 3rd 20 at 17:45
@Rachel_Lang, I Have a list of nested arrays in $adresses, I want to withdraw them cycle to be able to substitute a variable in the key. - Lilyan.Abbott commented on April 3rd 20 at 17:48
@Lilyan.Abbott, if you want to display a list of all nested arrays in $adresses, then the example above.
If you need some specific, for example Kazan, then
foreach($adreresses['kazan'] as $name => $phone){
 //actions $name or $phone
 echo $phone;
 }


Instead of 'kazan' is the variable to insert - Rachel_Lang commented on April 3rd 20 at 17:51

Find more questions by tags PHP