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 trial
Robert Young
11,742 PointsTricky code challenge - using a conditional in a foreach loop
Hi there,
I have managed to (bravely) reach the end of both PHP projects, but I am slightly stuck on the very last code challenge in the 'enhancing a php application' module.
It says, 'Right now, this block of code displays all five ice cream flavors that this store carries. Modify it to display only the flavors that are in stock'.
My code is as follows - I'm not sure that I am accessing the object properties correctly...
<?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);
foreach($flavors as $flavor => $value) {
if ($flavor=> in_stock = true){
echo $flavor["name"] . "\n";
}
}
?>
Any PHP wizards out there who might be able to help?!
Regards,
Robert
London, UK
Robert Young
11,742 PointsThanks Hampton! I understand how the code works better now.
Ok (after following Mansi's solution) I am receiving the message, "Hmmm ... the output looks correct, but I don't see the foreach loop. Did you remove it?".
At least it works, but not sure how to get it to pass...
Hampton Paulk
5,093 PointsRobert, paste your code at gist.github.com and lets take a look.
Robert Young
11,742 Points3 Answers
mansi gupta
2,337 PointsHi,
Please find the code below :
<?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);
foreach($flavors as $flavor => $value) {
if ($value["in_stock"] === true){
echo $value["name"] . "<br>";
}
}
?>
Regards, Mansi
Hampton Paulk
5,093 PointsThanks for all the information. Lets take a closer look at your code after the previous fixes.
foreach($flavors as $flavor => $value) {
if ($value["in_stock"] === true){
echo $value["name"] . "/n";
}
}
The first issue is the "/n" this needs to be switched back to "\n" as you had it in your previous code. You can dig into that topic here if you like.
Your values are correct but you are using too many options. You have no need it this challenge to test against the key as a separate argument inside of your foreach loop.
Instead of
foreach($flavors as $flavor => $value) {
if ($value["in_stock"] === true){
// output here
}
}
You can just use $flavor like this:
foreach($flavors as $flavor){
if($flavor["in_stock"] === true)){
// output here
}
}
This should get you through, and great job.
Hampton Paulk
5,093 PointsRobert Young take a look at the last code block above. You can use this to help your issue. Also in the code you put on github, you need to use "\n" and not "/n"
Dinakaran K
8,613 PointsI have added the code inthis link just get it..
Hampton Paulk
5,093 PointsHampton Paulk
5,093 PointsHi Robert,
The issue you are having, even though mansi has posted the solution below, is how you are trying to access a value inside of an associative array.
Take a few moments to look over the docs for accessing data from an associative array here paying special attention to the information on string keys.
This should help you in the future as well.