1

Supposing I have this array:

Array
(
    [NorthAmerica] => Array
        (
            [0] => Array
                (
                    [country] => Canada
                    [capital] => Ottawa
                )
            [1] => Array
                (
                    [country] => USA
                    [capital] => Washington
                )
        )
)

How can I loop into it to get NorthAmerica and capital ?

What I tried:

foreach($newDatas as $parent => $value) {
    echo $value; // should return NorthAmerica
    foreach($parent as $values) {
        echo $values['capital']; // should return Ottawa
    }
}

But it doesn't work.

Thanks for your help.

1 Answer 1

2

I think you are confusing the key and the value. Try:

foreach($newDatas as $parent => $value) {
    echo $parent; // should return NorthAmerica
    foreach($value as $values) {
        echo $values['capital']; // should return Ottawa
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.