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 Creating a Collection

György Varga
György Varga
19,198 Points

addRecipes challange question

Hi!

I made the "Challanges" in the teacher's note. Can you please look at them? And the last method "addRecipes" not working. I commented that out. Can you help me with that? I need to pass an array in the method, but I can't work it out. Here is the code: https://w.trhou.se/qtt9jlg63n

Thank you very much!

1 Answer

Lee Ravenberg
Lee Ravenberg
10,018 Points

Hey György, nice to see how far you progressed.

Here is the addRecipes() method you referred to.

class RecipeCollection
{
    private $recipes = array();

    public function addRecipes(array $recipes)
    {
      array_push($this->recipes, $recipes);
    }
}

Within the addRecipes you are using array_push(). This function takes anything and adds it to the end of the array.

The problem is that you are pushing the $recipes array that you passed as an argument to the end of the private $recipes array that serves as your collection. Thus making a two dimensional array.

The solution you are looking for is a way to merge the array you passed to the addRecipes() function to the existing $recipes array in your RecipeCollection. Make sure it only yields instances of your Recipe class.

György Varga
György Varga
19,198 Points

With the array_merge is working, fine, I think... Here is my solution: https://w.trhou.se/ofe8enz6zc

Lee Ravenberg
Lee Ravenberg
10,018 Points

Nice! Thats a really good use case for array_merge()!