Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTawan Prathomboon
2,734 PointsModify the foreach loop to display on the flavors that are in Stock.
<?php
$flavors = array(); $flavors[] = array("name" => "Cookie Dough", "in_stock" => true); $flavors[] = array("name" => "Vanilla", "in_stock" => false); $flavors[] = array("name" => "Avocado Chocolate", "in_stock" => false); $flavors[] = array("name" => "Bacon Me Crazy", "in_stock" => true); $flavors[] = array("name" => "Strawberry", "in_stock" => false);
//add your code below this line $stock = "true";
$order = array(); if ($stock == 'all'){ $order = array_keys($flavors); } else { foreach ($flavors as $name => $item) { if ($item["in_stock"] == $stock){ $order[] = $name ; } } } foreach ($order as $name){ echo $flavors[$name]["name"] . "<br />\n";
}
?>
Here is my php code. I ran it in the console and it show only the flavors that are in stock. But I cannot pass the code challenge. It should be more simple, but I don't know how.
<?php
$flavors = array();
$flavors[] = array("name" => "Cookie Dough", "in_stock" => true);
$flavors[] = array("name" => "Vanilla", "in_stock" => false);
$flavors[] = array("name" => "Avocado Chocolate", "in_stock" => false);
$flavors[] = array("name" => "Bacon Me Crazy", "in_stock" => true);
$flavors[] = array("name" => "Strawberry", "in_stock" => false);
//add your code below this line
$stock = "true";
$order = array();
if ($stock == 'all'){
$order = array_keys($flavors);
} else {
foreach ($flavors as $name => $item) {
if ($item["in_stock"] == $stock){
$order[] = $name ;
}
}
}
foreach ($order as $name){
echo $flavors[$name]["name"] . "<br />\n";
}
?>
2 Answers
Algirdas Lalys
9,389 PointsHi Tawan Prathomboon,
Yes indeed it should be simplier, you are doing something different then task asks us. Let me go through this challenge step by step.
Task 1 asks us "Using a foreach, create a loop that goes through each of the flavors in the $flavors array."
<?php
//add your code below this line
// Create foreach loop which goes throu $flavors array
foreach($flavors as $f) {
}
?>
Task 2 asks us "Inside of your foreach loop echo each of the flavors to the screen."
<?php
// Create foreach loop which goes throu $flavors array
foreach($flavors as $f) {
// Through $f temporary variable we can acces our associative array item with key 'name'
echo $f['name'] . "<br>";
}
?>
Task 3 asks us "Modify the foreach loop to display only the flavors that are in stock."
<?php
// Create foreach loop which goes throu $flavors array
foreach($flavors as $f) {
// Check if item is in stock if it's TRUE show it to the screen
if($f['in_stock']) {
// Through $f temporary variable we can access our associative array item with key 'name'
echo $f['name'] . "<br>";
}
}
?>
Jamal Shiekh
6,440 Points'''php This also work for me foreach($flavors as $flavor=>$value){ for($i = 0;$i <= count($flavors);$i++){ if ($value["in_stock"] === true){ echo $value[$i]; } } }
Tawan Prathomboon
2,734 PointsTawan Prathomboon
2,734 PointsThank you so much.
Cristian Opran
6,963 PointsCristian Opran
6,963 Pointsthanks