0

I've got this type of multidimensional array in PHP:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [Name] => France
                    [Capital] => Paris
                )

            [1] => Array
                (
                    [Name] => Italy
                    [Capital] => Rome
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [Name] => Canada
                    [Capital] => Ottawa
                )

        )

)

How can I loop into it ?

I try from my search on documentation:

foreach ($countries as $country)
{
  foreach ($country["Name"] as $name)
  {
     $capitals = array();
     foreach ($name["Capital"] as $capitals)
     {
       $capitals[] = $capital["Name"];
     }
     print implode(",", $capitals);
  }
}

Desired output should be:

Capital of `France` is `Paris`.
Capital of `Italy` is `Rome`.
Capital of `Canada` is `Ottawa`.

Could you please point me into the right direction ?

Thanks.

  • in your last for each you made mistake. jst var_dump it. – Devsi Odedra Nov 10 '17 at 04:07
  • Possible duplicate of [PHP Looping through array recursive](https://stackoverflow.com/questions/24259628/php-looping-through-array-recursive) – mikey Nov 10 '17 at 04:14
  • @mickmackusa: updated with the desired output. Thanks. –  Nov 10 '17 at 04:14
  • @mikeyjk you may retract your dupe link because for this question recursion is overkill. – mickmackusa Nov 10 '17 at 04:28
  • 1
    Possible duplicate of [how can i get multidimensional array values using foreach?](https://stackoverflow.com/questions/16356768/how-can-i-get-multidimensional-array-values-using-foreach) and [How to get values of multidimensional array using a loop](https://stackoverflow.com/questions/20018643/how-to-get-values-of-multidimensional-array-using-a-loop) and [How to loop in a multidimensional array in PHP?](https://stackoverflow.com/questions/42622134/how-to-loop-in-a-multidimensional-array-in-php) – mickmackusa Nov 10 '17 at 07:14

2 Answers2

0

You must use two loops to access the lowest subarray, then you can access the values by their keys (Name and Capital).

Code: (Demo)

$array=[
    1=>[
        ['Name'=>'France','Capital'=>'Paris'],
        ['Name'=>'Italy','Capital'=>'Rome']
    ],
    2=>[
        ['Name'=>'Canada','Capital'=>'Ottawa']
    ]
];

foreach($array as $subarray){
    foreach($subarray as $subset){
        echo "Capital of `{$subset['Name']}` is `{$subset['Capital']}`.<br>";
    }
}

Output:

Capital of `France` is `Paris`.
Capital of `Italy` is `Rome`.
Capital of `Canada` is `Ottawa`.
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

just update the foreach loop

foreach ($countries as $country)
{
  foreach ($country as $cdata)
  {
     echo "Capital of '".$cdata['Name']."' is '".$cdata['Capital']."'<br/>".
  }
}
romal tandel
  • 481
  • 12
  • 19
  • This is a late duplicate of my method, therefore it adds no value to the page. – mickmackusa Nov 10 '17 at 04:32
  • when i posted your answer was not showing my side. thats why i posted. i didn't mean to post duplicate answer – romal tandel Nov 10 '17 at 05:34
  • That is understandable, but the fact remains that it is a late duplicate. You should delete it on principle, but I can see that you have received an undermining upvote so now you are less likely to do the right thing. Thanks Upvote Pixies. – mickmackusa Nov 10 '17 at 06:16