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

Alfredo Prince
Alfredo Prince
6,175 Points

I really don't understand what I should be doing here.

I returned the array but the error says the return value should be an array. should i be using a foreach loop to go through the collection of houses? I am lost.

index.php
<?php

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

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

1 Answer

Alfredo,

This is a confusing challenge. Here are a few details that the challenge doesn't give you that you need to know:

  1. The houses property is an array of House objects.
  2. The House class has three public member variables: lot, roof_color, and wall_color.

You are supposed to loop through the houses array and create a separate array that only contains the House objects that have a roof_color or wall_color equal to the color passed to the filterHouseColor method.

The result looks something like this:

<?php

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

public function filterHouseColor($color)
{
    $matches = array();
    for($i=0;$i<count($this->houses);$i++){
        if ($this->houses[$i]->roof_color == $color || $this->houses[$i]->wall_color == $color) {
            $matches[] = array('lot' => $this->houses[$i]->lot); 
        }
    }
    return $matches;
}

} ?>

Alfredo Prince
Alfredo Prince
6,175 Points

Yea they don't really give me much to work with and I feel tricked into thinking that what I have on screen is what i should work with.

Alfredo Prince
Alfredo Prince
6,175 Points

How is it that you decided to use a for loop instead of foreach? This challenge really didn't give me much to work with. I could have gotten to the same conclusion but these challenges are usually very rigid in the way they want it done.

In reality, either option would work as well as the other. Initially, I tried a foreach loop, but the challenge didn't accept my code. I'm sure if it was because of the foreach loop, but I dropped in a for loop instead with maybe another change or two, and it worked.