Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Object-Oriented PHP Basics Building a Collection Collection Objects

Return an collection array of house objects whose public properties roof_color OR wall_color match the passed color para

Return an collection array of house objects whose public properties roof_color OR wall_color match the passed color parameter. Note: The local houses array contains an array of House objects, each of which has a public property for "roof_color" and "wall_color".

index.php
<?php
class Subdivision {
  public $houses = []; // Same thing as array()
  public function filterHouseColor($color) {
    $lots = []; // just a temporary array 

    // Looks like this is where you got lost
    // In order to access the $houses array, you need to do $this->houses because $houses is a property on Subdivision
    // This foreach loop loops through the $houses array
    // So each cycle, $house contains a $house object found in $houses
    foreach ($this->houses as $house) {
      // Remember: you can only access $roof_color and $wall_color from the $house object
      if ($house->roof_color === $color || $house->wall_color === $color) {
        $lots[] = $house->lot; // Here, we append the value of each $house's $lot property to that temporary array up there named $lots
      }
    }

    return $lots;  // Finally, we return $lots
  }
}
?>
Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

What is the problem you are running into? I am not very fluent in PHP so I can't help, but this will help others trying to solve your problem.

1 Answer

Mikkel Rasmussen
Mikkel Rasmussen
31,769 Points

You just need to change your append value in your if statement from $house->lot to just $house.