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 trialGareth Redfern
36,217 PointsObject-Orientated PHP Code Final Code Challenge - Collection Objects
Hi there I am really stuck with this question it seems to jump from creating a property and a method on the class which I get no problem to then access a house object with a let property?
<?php
// add code below this comment
class Subdivision
{
public $houses = array();
public function filterHouseColor($color)
{
}
}
?>
3 Answers
jcorum
71,830 PointsGareth, something along these lines:
class Subdivision {
public $houses = [];
public function filterHouseColor($color) {
$lots = []; //create array
foreach ($this->houses as $house) { //loop through array
if ($house->roof_color === $color || $house->wall_color === $color) {
$lots[] = $house->lot;
}
}
return $lots;
}
}
Gareth Redfern
36,217 PointsThank you jcorum, I was going down the road of trying to new up a house object and then add it to the houses array because I thought I would need a house object to work with. Is it me or is that question not very clear?
Your code worked great thank you.
Denis Rubanga
2,907 Points<?php
// add code below this comment class Subdivision { public $houses = [];
public function filterHouseColor($color) { $lots = []; //create array
foreach ($this->houses as $house) { //loop through array
if ($house->roof_color === $color || $house->wall_color === $color) {
$lots[] = $house;
}
}
return $lots;
} }
?>
Denis Rubanga
2,907 Points$lots[] = $house->lot; needs to be changed to $lots[] = $house;
Gareth Redfern
36,217 PointsGareth Redfern
36,217 PointsThis is the question:
Return an array containing the public
lot
parameter of each house object whose publicroof_color
ORwall_color
match the passedcolor
parameter.