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

PHP Object-Oriented PHP Basics Building a Collection Collection Objects

Dan Sternbach
Dan Sternbach
7,386 Points

Trying to complete the challenge

I created an array and am looping thru each object to check if the house->color or house->wall_color is equal to $color:

index.php
<?php

// add code below this comment
class Subdivision {
    public $houses = array(); 
    function filterHouseColor($color)
    {
        foreach ($this->houses as $house) {
          if ($house->roof_color == $color || $house->wall_color == $color)
          {
            $houses[]=$house;
            return $houses;
          }
        }
    }
}

2 Answers

Algirdas Lalys
Algirdas Lalys
9,389 Points

Hi Dan Sternbach

Hmm its very complicated it seems that you are storing your values in the $houses array, I would create temporary array for storing houses and then returned the values. So it would be something like that.

<?php
    // Temporary house array
    $housesReturned = array();
    foreach($this->houses as $house)
    {
      if($house->roof_color == $color || $house->wall_color == $color) {
        $housesReturned[] = $house;
      }
    }
    // After foreach() returning array
    return $housesReturned;
?>
Dan Sternbach
Dan Sternbach
7,386 Points

The answer above does not complete the challenge as the $housesReturned array needs to be renamed $houses per challenge question #1. I was able to get thru the challenge with a few edits to the above.

Algirdas Lalys
Algirdas Lalys
9,389 Points

Hmm strange it works for me. Could you paste your final code?