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

Help with wall_color and roof_color code challenge

The challenge say to compare the parameter pass ($color) to the roof and wall color and record the house's lot number to an array and return it.

As I thought this was pretty straight forward, my code is below:

<?php

// add code below this comment class Subdivision{ public $houses = array();

public function filterHouseColor($color){ $lot = array(); if($house->roof_color == $color || $house->wall_color == $color){ $lot[] = $house->lot; }
return $lot; } }

?>

Thanks in advance for the help!

index.php
<?php

// add code below this comment
class Subdivision{
  public $houses = array();

  public function filterHouseColor($color){
    $lot = array();
    if($this->house['roof_color'] == $color || $this->house['wall_color'] == $color){
        $lot[] = $house->lot;
    }    
    return $lot;
  }
}

?>

My original code is below:

<?php

// add code below this comment class Subdivision{ public $houses = array();

public function filterHouseColor($color){ $lot = array(); if($house->roof_color == $color || $house->wall_color == $color){ $lot[] = $house->lot; }
return $lot; } }

?>

1 Answer

Simon Coates
Simon Coates
28,693 Points

A lot of people have been having trouble with that challenge in the last couple days. Try something like

  public function filterHouseColor($color)
 {
    $arr = [];
     foreach($this->houses as $ahouse)    
    {
      if($ahouse->roof_color === $color ||  $ahouse->wall_color === $color)     {
       $arr[] = $ahouse->lot;
      }
    }
    return $arr;
  }

Thanks Simon!