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 PHP Arrays and Control Structures PHP Loops For Looping

Keith Colleluori
Keith Colleluori
9,577 Points

I am finding task number two very difficult. My problem is displaying the facts from the array next to the #s

I am asked to loop through the numbers requested and then in task 2 find where the looping numbers match the key of an associative array and print out the value.

I am not sure if it is necessary but I have ksorted the $facts into order:

ksort($facts);

and completed task one with my loop:

for ($i = 1; $i <= 100; $i++) { echo $i . "<br />\n"; }

Then for task two I insert the following statement above the echo $i

if (isset($facts[$i]));

And then I draw a blank... Any help would be appreciated!! The following is the script from the task.


<?php $facts = array( 57 => ' on Heinz ketchup bottles represents the number of varieties of pickles the company once had.', 2 => ' is the approximate hours a day Giraffes sleeps', 18 => ' is the average hours a Python sleeps per day', 10 => ' per cent of the world is left-handed.', 11 => ' Empire State Buildings, stacked one on top of the other, would be required to measure the Gulf of Mexico at its deepest point.', 98 => '% of the atoms in your body are replaced every year', 69 => ' is the largest number of recorded children born to one woman', );

ksort($facts);

for ($i = 1; $i <= 100; $i++) { if (isset($facts[$i]));
echo $i . "<br />\n"; }

index.php
<?php
$facts = array(
    57 => ' on Heinz ketchup bottles represents the number of varieties of pickles the company once had.',
    2 => ' is the approximate hours a day Giraffes sleeps',
    18 => ' is the average hours a Python sleeps per day',
    10 => ' per cent of the world is left-handed.',
    11 => ' Empire State Buildings, stacked one on top of the other, would be required to measure the Gulf of Mexico at its deepest point.',
    98 => '% of the atoms in your body are replaced every year',
    69 => ' is the largest number of recorded children born to one woman',
);
//add your loop below this line

1 Answer

Hi Keith, you got it. Now that you saw if the $facts[$i] is set, you just need to echo it inside the if statement. Put the if statement below the previous echo $i;

For this challenge, you don't need to sort the keys.

for ($i = 1; $i <= 100; $i++) {
    echo $i ; 
    if (isset($facts[$i])) {
        echo $facts[$i];
    }
}
Keith Colleluori
Keith Colleluori
9,577 Points

thanks for the help. I was so close and then, yet even when I tried to type in what you said it was still hanging up. Then after copy and paste, bang it worked... of course that overwrote what I wrote so I cant find the error, but I understand, so I'm not gonna get hung up about it. Thanks again!!!

Glad to know that helped. A few things that I noticed from your code, and I don't know if that was the problem, but was that you used a semicolon after the if statement if (isset($facts[$i])); You should replace it for an opening and closing curly braces. Another thing was that you mentioned you placed the if statement above the echo $i. That should go below it.

I'm not sure if that was the problem, but just a few ideas

Keith Colleluori
Keith Colleluori
9,577 Points

I have a feeling you might be right. A couple times I have caught myself trying to add the semi-colon in places in addition to the curly brace. I guess thats a little confusing sometimes, because the general idea is that everything in php ends with the semi. But I'll just need to be careful that it doesn't apply at the end of a line which remains open via {}. Even if for instance a for loop has multiple semis within, it does not end with one because it remains open till the final conditional: for ($i=5; $i<10; $i++) { words; words; }

and then at the end no semi except in last conditional