Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Listing Inventory Items Nesting Arrays Within Arrays

foreach()

Hello!

Can you please run this code and see why it is not displaying both country arrays (US, and DE). It is only displaying the elements of Germany.

<?php

$countries = array();

$countries[0] = array(
"code"=> "US",
"name"=> "United States of America",
"capital" => "WA",
"population" => 225000000

);

$countries[1] = array(
"code"=> "DE",
"name"=> "Germany",
"capital" => "Berlin",
"population" => 2250000000

);

?>

<h1><?php echo $countries[0]['name'] ?></h1>
<dl>
<?php foreach($countries as $country)?>
    <dt> Country:</dt>
    <dd><?php echo $country['code'];?></dd>

    <dt> Capital :</dt>
    <dd> <?php echo $country['capital']; ?></dd>


    <dt> Population : </dt>
    <dd> <?php echo $country['population']; ?></dd>
</dl>

5 Answers

Hello Colin,

I have a foreign keyboard, and I always use ~~~ to post, I am not sure what happened this time, but I will see if this problem occurs again. Thanks for pointing that out, though!!

As for the code, the reason it wasn't working is because I put this line below outside the loop, right? <h1><?php echo $countries[0]['name'] ?></h1> So should make sure I have the same number of elements in my loop as in my array?

Cheers!

Colin Marshall
Colin Marshall
32,861 Points

No, the reason it wasn't working was because you were missing the curly braces at the beginning of the loop and at the end. Curly braces = { }

The opening line of your loop looked like this:

<?php foreach ($countries as $country)?>

It should like this:

<?php foreach ($countries as $country) { ?>

And you were missing this line at the very end:

<?php } ?>
Colin Marshall
Colin Marshall
32,861 Points

You're missing the foreach loop part. Wrap the html in the loop:

<?php foreach ($countries as $country) { ?>

    <dt> Country:</dt>
    <dd><?php echo $country['code'];?></dd>

    <dt> Capital :</dt>
    <dd> <?php echo $country['capital']; ?></dd>


    <dt> Population : </dt>
    <dd> <?php echo $country['population']; ?></dd>

<?php } ?>

Hello Collin,

The foreach is missing?? but it is there: Oh, wait it is not in the first code... but even with the code below, the info of both countries don't appear on the screen?

thansk!

<?php

$countries = array();

$countries[0] = array(
"code"=> "US",
"name"=> "United States of America",
"capital" => "WA",
"population" => 225000000

);

$countries[1] = array(
"code"=> "DE",
"name"=> "Germany",
"capital" => "Berlin",
"population" => 2250000000

);

?>

<h1><?php echo $countries[0]['name'] ?></h1>
<dl>
<?php foreach($countries as $country)?>
    <dt> Country:</dt>
    <dd><?php echo $country['code'];?></dd>

    <dt> Capital :</dt>
    <dd> <?php echo $country['capital']; ?></dd>


    <dt> Population : </dt>
    <dd> <?php echo $country['population']; ?></dd>



</dl>
Colin Marshall
Colin Marshall
32,861 Points

Ahh I see you did have the foreach loop in there. You inserted the code incorrectly so some of your code didn't show up.

Your problem is that you're missing the curly braces in the loop. See my example again.

When you are putting your code on the forum, you surround it with tick marks (```). You had your code surrounded in tilde's (~~~). They're on the same key on the keyboard.

Colin Marshall
Colin Marshall
32,861 Points

And one more thing... You'll want to take this line:

<h1><?php echo $countries[0]['name'] ?></h1>

Move it inside of the loop, and change it to:

<h1><?php echo $country['name'] ?></h1>

So it would look like this:

<?php foreach ($countries as $country) { ?>

    <h1><?php echo $country['name']; ?></h1>

    <dt> Country:</dt>
    <dd><?php echo $country['code'];?></dd>

    <dt> Capital :</dt>
    <dd> <?php echo $country['capital']; ?></dd>


    <dt> Population : </dt>
    <dd> <?php echo $country['population']; ?></dd>

<?php } ?>

Got it! Thank you Colin!