5. Iterating Over Associative Arrays


#1

I can not find the error, why doesn't this work?
As soon as i enter the site the terminal window starts spinning and as soon as i press Submit code i get
"The program took too long to finish. Check your code for infinite loops and try again."

I'd super appreciate it of someone posted working code and explain why mine doesn't work.

Thanks!

<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 />';
    
      // Create your own array here and loop
      // through it using foreach!
      $pcParts = array('cpu' => 'Intel', 'gpu' => 'Nvidia','mb' => 'Asus');
      foreach ($pcParts as $parts=>$include) 
      {
          echo $include . ' ' . $parts . '<br />';
      }

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


#2

I appear to have solved it with

      $myPcParts = array('cpu' => 'Intel', 'gpu'=>'Nvidia', 'mb'=>'Asus');
      foreach ($myPcParts as $hardware=>$parts) {
          echo 'My ' . $hardware . ' is ' . $parts . '. ';
      }

$parts is the cpu, gpu and mb,
and $parts is Intel, nvidia and asus, so it displays both strings.

It still feels like the compiling is broken af though?
Since i had to use another browser
in order to get it to work


#3

HI I tried you code and it works ..... you needed only to refresh the page


#4

I refreshed the page more than 10 times xD
It worked when i switched browser though

Edit: I also get a random error saying: The program took too long to finish. Check your code for infinite loops and try again

eventhough i never pressed the " Save $ Submit Code " button?!?


#5

I pass it with Chrome:



Iteration Nation



<?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 />';

  // Create your own array here and loop
  // through it using foreach!
$me = array('hair' => 'black',
        'skin tone' => 'light');

foreach ($me as $feature=>$colour) {

echo 'My ' . $feature . ' is ' . $colour . '. ';
}

  ?>
</p>