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

Alex Bauer
Alex Bauer
9,426 Points

Can someone please help me understand what I should be doing?

Instructions: 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".

I've tried many different ways to do this and I just don't get what I'm doing wrong.

index.php
<?php

// add code below this comment
class Subdivision
{
        public $houses;

    public function filterHouseColor($color)
    {

    }
}

?>

1 Answer

Hey,

Try the below:

<?php

class Subdivision {
  public $houses;

  public function filterHouseColor($color) {
    // Use array_filter to filter the $houses array to only those whose roof_color 
    // or wall_color properties matches the passed $color argument.
    //
    // Make sure to add the `use ($color)` bit to access the $color argument.
    // https://stackoverflow.com/questions/2408908/is-it-possible-to-access-outer-local-variable-in-php
    $filteredHouses = array_filter($this->houses, function($house) use ($color) {
        return $house->roof_color === $color || $house->wall_color === $color;
    });

    return $filteredHouses;
  }
}

?>

Good luck with your coding journey!

Alex Bauer
Alex Bauer
9,426 Points

That worked wow thank you! I wish I knew what they were trying to make me do but I'm going to learn how the code that you used makes sense.