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

Cory Jaccino
seal-mask
.a{fill-rule:evenodd;}techdegree
Cory Jaccino
Full Stack JavaScript Techdegree Student 2,842 Points

Are we cheching ANY value of $facts?

Task 2 of 2 asks us to, "[...] test if the incremented value equals one of the keys in the $facts array." Does this mean ANY value within the $facts array? If so, then I believe we should loop through the $facts array as well.

After checking to make sure that $facts is set, I'm unsure if I'm solving this in the way that the task wants. I think this question should be clarified a little further. I've tried to comment each logical step. What are your thoughts?

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
// Create a for loop that displays each number from 1 to 100
for ($i = 1; $i <= 100; $i++) {
  echo $i;
// Use isset function to check to make sure that $facts is set
    if(isset($facts)) {
// Loop through each value of $facts
      for ($j = 1; $j <= count($facts); $j++) {
// Test if the incremented value $i equals any one of the keys in the $facts array
        if ($i = $facts[$j]) {
          echo $j;
        }
      }
    }
  }
}

3 Answers

Steven Parker
Steven Parker
231,268 Points

Your concept is valid, it would pass the challenge after fixing two issues:

  • there's a stray extra closing brace that should be removed
  • the equality test operator is "==" ("=" is the assignment operator)

But you don't need a separate loop to check for the key. The "isset" function will tell you if there is a value set for the key by itself:

<?php
// ...
for ($i = 1; $i <= 100; $i++) {
  echo $i;
  // Test if the incremented value $i equals any one of the keys in the $facts array
  if (isset($facts[$i])) {
    echo $facts[$i];
  }
}
Cory Jaccino
seal-mask
.a{fill-rule:evenodd;}techdegree
Cory Jaccino
Full Stack JavaScript Techdegree Student 2,842 Points

Doh! Thank you, Steven!

I was so focused on the loop that I started glossing over the syntax! 😱(Note to self!) 😅

Thanks again for your quick response. :-)

Cory

Steven Parker
Steven Parker
231,268 Points

Glad to help. Remember to mark the question solved, and happy coding!