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

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

Don't understand this part...

Hi!

Can you please help me understand this part:

<?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;
    }

If at the end we add the "amount" and "measure" why it does not display it? Thanks for your help!

Robert Leonardi
Robert Leonardi
17,151 Points

Can you provide the sample output array of $this->recipes and $recipe->getIngredients() ?