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

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,694 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!