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 Shopping List Method

Egg & Eggs are both being displayed

I believe my code is the same as the instructors. My code below. It returns the same results when getting the shopping list on $breakfast, but if I am to get a shopping list for the entire $cookbook there are some glitches. Something seems to be wrong here.

Any ideas?

<?php
public function getCombinedIngredients()
    {
        $ingredients = array();
        foreach($this->recipes as $recipe){
            foreach($recipe->getIngredients() as $ing){
                $item = $ing["item"];
                if(strpos($item, ",")){
                    $item = strstr($item, ",", true);
                }
                if(in_array($item."s", $ingredients)){
                    $item.="s";
                } else if(in_array(substr($item, 0, -1), $ingredients)){
                    $item = substr($item, 0, -1);
                }
                $ingredients[$item] = array(
                    $ing["amount"],
                    $ing["measure"]
                );
            }
        }
        return $ingredients;
    }

PS. I even cut n' paste teachers code from the teachers notes and still exact same results. Duplicates are being returned when applying to $cookbook.

Examples: Egg Eggs Onion Onions

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

jaycode,

I wish I could help you out, but my PHP is incredibly rusty and I wouldn't know where to start. Hope you find the answer!

4 Answers

If we use array_key_exists() instead of in_array() this will solve one part of the problem since our ingredients are stored as an array key, instead of inside of the array.

if(array_key_exists($item."s", $ingredient_list)) {
         $item .= "s";
    } else if (array_key_exists(substr($item, 0 , -1), $ingredient_list)) {
         $item = substr($item, 0, -1);
    }

I'm not 100% sure it's the best way, but it appears to work.

Alena Holligan
Alena Holligan
Treehouse Teacher

The function should be "array_key_exists" to look at the "key" of the array. The function "in_array" will check for a "value" in the array.

//check if there is already a ingredient WITH the "s"
    if(array_key_exists($item."s", $ingredient_list)) {
//if so, ADD the "s" so there is a single item
         $item .= "s";
//check if there is an ingredient without the "s"
    } else if (array_key_exists(substr($item, 0 , -1), $ingredient_list)) {
//if so, REMOVE the "s" so there is a single item
         $item = substr($item, 0, -1);
    }

I am wondering if Alena Holligan or anyone else can provide insight to this question. It was asked 2 months ago but no replies. Please help!

I don's specifically understand this portion of the code

if(in_array($item."s", $ingredients)){
                    $item.="s";
                } else if(in_array(substr($item, 0, -1), $ingredients)){
                    $item = substr($item, 0, -1);
                }

Why would $item."s" be in the array? For example, if we are looking for "Eggs", wouldn't $item."s" search for "Eggss" with 2 s at the end?

Wouldn't it make more sense to see if the last character of the item ends in "s"? Something like this....

if(substr($item, -1) == "s")

I am totally lost, please help!!!

Alena Holligan please help. There are no answers on any of the questions for this video. Several people have the same problem as me. I've been stuck on this topic for last 2 weeks and need to move forward. Please help.