Jump to content

sittiponder

New Members
  • Posts

    5
  • Joined

  • Last visited

sittiponder's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Then how can I get your idea to work? It may just have been that the codecademy editor is glitchy, and I'm sure I'll use arrays within arrays in the future, but I'd like to know what was wrong with it.
  2. 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.
  3. 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
  4. 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>
  5. This is for a class on codecademy. I'm getting a parse error for an unexpected T_variable on line 35. I'm sure it's a simple mistake. I'm new to coding.
×
×
  • 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.