sittiponder Posted September 18, 2013 Share Posted September 18, 2013 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> Link to comment https://forums.phpfreaks.com/topic/282246-why-is-my-array-foreach-not-displaying-correctly/ Share on other sites More sharing options...
dungpt29 Posted September 18, 2013 Share Posted September 18, 2013 You should replace your last loop with the following: foreach ($candy as $type => $specific) { if ($type == 'chocolate') { echo $specific . "<br />"; } else { echo $type . ' ' . $specific; } } Link to comment https://forums.phpfreaks.com/topic/282246-why-is-my-array-foreach-not-displaying-correctly/#findComment-1450062 Share on other sites More sharing options...
vinny42 Posted September 18, 2013 Share Posted September 18, 2013 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')); Link to comment https://forums.phpfreaks.com/topic/282246-why-is-my-array-foreach-not-displaying-correctly/#findComment-1450063 Share on other sites More sharing options...
sittiponder Posted September 18, 2013 Author Share Posted September 18, 2013 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 Link to comment https://forums.phpfreaks.com/topic/282246-why-is-my-array-foreach-not-displaying-correctly/#findComment-1450066 Share on other sites More sharing options...
sittiponder Posted September 18, 2013 Author Share Posted September 18, 2013 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. Link to comment https://forums.phpfreaks.com/topic/282246-why-is-my-array-foreach-not-displaying-correctly/#findComment-1450067 Share on other sites More sharing options...
vinny42 Posted September 18, 2013 Share Posted September 18, 2013 That's not solving the problem, that making a big mess and not paying attention to what you are doing. Indexes are not data. Link to comment https://forums.phpfreaks.com/topic/282246-why-is-my-array-foreach-not-displaying-correctly/#findComment-1450073 Share on other sites More sharing options...
sittiponder Posted September 18, 2013 Author Share Posted September 18, 2013 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. Link to comment https://forums.phpfreaks.com/topic/282246-why-is-my-array-foreach-not-displaying-correctly/#findComment-1450079 Share on other sites More sharing options...
PaulRyan Posted September 18, 2013 Share Posted September 18, 2013 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>'; } } } ?> Link to comment https://forums.phpfreaks.com/topic/282246-why-is-my-array-foreach-not-displaying-correctly/#findComment-1450082 Share on other sites More sharing options...
vinny42 Posted September 18, 2013 Share Posted September 18, 2013 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); Link to comment https://forums.phpfreaks.com/topic/282246-why-is-my-array-foreach-not-displaying-correctly/#findComment-1450085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.