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

Justin Radcliffe
Justin Radcliffe
18,987 Points

I don't follow some of the logic - could someone clarify?

public function getCombinedIngredients() 
    {
       $ingredients = [];
       foreach ($this->recipes as $recipe) {
           foreach ($recipe->getIngredients() as $ing) {
               $item = $ing['item'];
               // if item string contains a ','
               if (strpos($item, ',')) {
                   // select string BEFORE (using true boolean) the ',' (if i wanted AFTER the ',' don't use true boolean)
                   $item = strstr($item, ',', true);
               }

// START OF LOST FEELING!

               // if item is plural (contain an 's' on the end)
               if (in_array($item.'s', $ingredients)) {
                   $item .= 's';
               } elseif (in_array(substr($item, 0, -1), $ingredients)) {
                   $item = substr($item, 0, -1);
               }

// END OF LOST FEELING!

               $ingredients[$item] = [
                   $ing['amount'],
                   $ing['measure'],
               ];
           }
       }
       return $ingredients;
    }

2 Answers

I feel lost for the exact same portion of the code. If anyone can explain it would be great!

The first part on the second part also is not clear to me, specifically "$item .= 's';"

The second part of that block seems like it filters plural items and only passes singular ingredients to $item