-1

I have this array ($resultPerCountry) that I built from a simple SQL request (then I group the items by country, resulting with this multidimensional array :

Array
(
    [Denmark] => Array
        (
            [0] => Array
                (
                    [name] => foo
                    [logo] => foo.png
                    [country] => Denmark
                )
            [1] => Array
                (
                    [name] => bar
                    [logo] => bar.png
                    [country] => Denmark
                )
        )
    [Finland] => Array
        (
            [0] => Array
                (
                    [name] => baz
                    [logo] => baz.png
                    [country] => Finland
                )
        )
)

How can I display it so that I have something like this in HTML :

<strong>Denmark</strong>
<p>Foo</p>
<p>Bar</p>
<strong>Finland</strong>
<p>Baz</p>

I'm trying with a simple foreach but i'm getting lost...

Thanks !

1
  • Have nested loops? Commented Mar 6, 2017 at 9:41

2 Answers 2

3

Just to show an alternative syntax to @GuillaumeSTLR answer.

<?php foreach ($array as $location => $items) : ?>

    <strong><?php echo $location ?></strong>

    <?php foreach ($items as $item) : ?>
        <p><?php echo $item ?></p>
    <?php endforeach; ?>


<?php endforeach; ?>

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

2

You have to use 2 foreachs, like this :

<?php

foreach($array as $country => $infos) {
    echo sprintf("<strong>%s</strong>", $country);
    foreach($infos as $info) {
        echo sprintf("<p>%s</p>", $info['name']);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.