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 trialGyörgy Varga
19,198 PointsaddRecipes 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
10,018 PointsHey 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
19,198 PointsGyörgy Varga
19,198 PointsWith the array_merge is working, fine, I think... Here is my solution: https://w.trhou.se/ofe8enz6zc
Lee Ravenberg
10,018 PointsLee Ravenberg
10,018 PointsNice! Thats a really good use case for
array_merge()
!