Jump to content

Why is my array foreach not displaying correctly?


sittiponder

Recommended Posts

Check out my third array. It's only printing out "Reese's" and "gummy worms."

I want it to display "M&M's," "Snickers," "Reese's," "gummy bears," and "gummy worms."

I'm doing this for my codecademy course. The important part starts on line 33.

I'm new to coding.

<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!
    $candy = array('chocolate'=> 'M&Ms', 
                'chocolate'=>'Snickers', 
                'chocolate'=>'Reese\'s', 
                'gummy'=>'bears', 
                'gummy'=>'worms');
    foreach ($candy as $type => $specific):
        if ($type == 'chocolate'){
            echo $specific."<br></br>";
        } else {
            echo $type . ' ' . $specific;
        }
        continue;
    endforeach;
      ?>
    </p>
  </body>
</html>

The syntax array('foo'=>'bar') assigns 'bar' to the index 'foo'. There can be only one value per index, so you are overwriting 'snickers' with 'reese' and 'bears' with 'worms'.

 

It seems like you need to go one deeper: array('chocolate'=>array('snickers','reese'), 'gummy'=>array('bears','worms'));

Now I have

    $candy = array('chocolate' => array('M&Ms', 'Snickers','Reese\'s'), 'gummy' => array('bears', 'worms'));
    foreach ($candy as $type => $specific)
    {
        if ($type == 'chocolate')
        {
            echo $specific."<br />";
        } 
        else 
        {
            echo $type . ' ' . $specific;
        }
    }

And it prints 

 

Array

gummy Array

I fixed it by switching $type and $specific.

    $candy = array('M&Ms' => 'chocolate' , 
                'Snickers' => 'chocolate', 
                'Reese\'s' => 'chocolate', 
                'bears' => 'gummy', 
                'worms' => 'gummy');
    foreach ($candy as $type => $specific)
    {
        if ($specific == 'chocolate')
        {
            echo $type."<br />";
        } 
        else 
        {
            echo $specific . ' ' . $type . "<br />";
        }
    }

Thanks for the help.

You are making more work for yourself, if you have to repeat data, it's probably not correctly written.

 

Something like this:

 

<?PHP

  $candy = array('chocolate' => array('Mars', 'M&Ms', 'Reese\'s'),
                 'gummy'     => array('Worms', 'Bears'));
                 
  foreach($candy AS $type => $typeArray) {
 
    if($type == 'chocolate') {
      foreach($typeArray AS $specific) {
        echo $specific.' <br>';
      }
    } else {
      foreach($typeArray AS $specific) {
        echo $specific.' '.$type.' <br>';
      }
    }
    
  }
 
?>

When you have an array that contains arrays, you have to process the sub-arrays as arrays, not just print them because when you do echo(array('foo')); you will get "Array".

 

So, you'd get someting like:

 

$arrArray = array('chocolate'=>array('mint','bar','rabbit));

foreach($arrArray as $strKey=>$arrContent)

{

echo $strkey.' contains: ';

 foreach($arrContent as $strstuff)

 {

  echo $strStuff .' ';

 }

echo "<br/>";

}

 

Or, to see what you are dealing with:

var_dump($arrArray);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.