Can't progress but it works?


#1

  1. Iterating Over Associative Arrays

The code works and prints out in the console, but I can't progress!

Oops, try again. It looks like you didn't print any new text! Make sure to loop over your array and print or echo its contents to the page.


<html>
  <head>
    <title>Iteration Nation</title>
  </head>
  <body>
    <p>
      <?php
$food  = array(
    'pizza',
    'salad',
    'burger'
);
$salad = array(
    'lettuce' => 'with',
    'tomato' => 'without',
    'onions' => 'with'
);

// Looping through an array using "for".
// First, let's get the length of the array!
$length = count($food);

// Remember, arrays in PHP are zero-based:
for ($i = 0; $i < $length; $i++)
  {
    echo $food[$i] . '<br />';
  }

echo '<br /><br />I want my salad:<br />';

// Loop through an associative array using "foreach":
foreach ($salad as $ingredient => $include)
  {
    echo $include . ' ' . $ingredient . '<br />';
  }

echo '<br /><br />';

$array1 = array(
    "great dane",
    "staffie",
    "pug",
    "labrador",
    "dalmation"
);
$array2 = array(
    "olly" => "burman",
    "lily" => "siamese",
    "hamish" => "ragdoll",
    "salem" => "black",
    "beau" => "persian"
);

for ($i = 0; $i < count($array1); $i++)
  {
    echo "<li>" . $array1[$i] . "</i>";
  }

echo "<p>";

foreach ($array2 as $name => $breed)
  {
    echo "<li>" . $name . " is a " . $breed . " cat </li>";
  }

?>
    </p>
  </body>
</html>


#2

Managed to get it working. Just /* */ commented out the example code so that it was just my code in there.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.