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

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.