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

Leandro Roberto
Leandro Roberto
3,574 Points

How to return array object

I don't understand the question. the $this->houses array has another array of objects. How to check the wall_color and roof_color value and how to return this array?

index.php
<?php

// add code below this comment

class Subdivision
{
  public $houses = array();

  public function filterHouseColor($color)
  {
      foreach($this->houses as $house)
      {
        if ($house->roof_color == $color || $house->wall_color == $color)
        {
          return $house;
        }

      }
  }

}


?>
Mark Miller
Mark Miller
45,831 Points

I'm stuck on this one also. But I see one mistakes in yours. You are returning only one match instead of continuing to loop through the entire array. The return statement terminates the for-each loop and exits the function immediately, so any remaining elements of the array go unchecked by your code. You should store each match in another array, then return that entire array of objects, the collection, after the for-each has checked them all.