Advertisement
ujiajah1

Iterating Over Associative Arrays

Aug 22nd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. <html>
  2.   <head>
  3.     <title>Iteration Nation</title>
  4.   </head>
  5.   <body>
  6.     <p>
  7.       <?php    
  8.         $food = array('pizza', 'salad', 'burger');
  9.         $salad = array('lettuce' => 'with',
  10.                    'tomato' => 'without',
  11.                    'onions' => 'with');
  12.    
  13.       // Looping through an array using "for".
  14.       // First, let's get the length of the array!
  15.       $length = count($food);
  16.    
  17.       // Remember, arrays in PHP are zero-based:
  18.       for ($i = 0; $i < $length; $i++) {
  19.         echo $food[$i] . '<br />';
  20.       }
  21.    
  22.       echo '<br /><br />I want my salad:<br />';
  23.    
  24.       // Loop through an associative array using "foreach":
  25.       foreach ($salad as $ingredient=>$include) {
  26.         echo $include . ' ' . $ingredient . '<br />';
  27.       }
  28.    
  29.       echo '<br /><br />';
  30.    
  31.       // Create your own array here and loop
  32.       // through it using foreach!
  33. $array_kamu = array(1610, 'VOC Belanda', 350);
  34.       foreach ($array_kamu as $ingredient=>$include) {
  35.         echo $include . ' ' . $ingredient . '<br />';
  36.       }
  37.  
  38.       ?>
  39.     </p>
  40.   </body>
  41. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement