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

Jeff Styles
Jeff Styles
2,784 Points

can't get same output with code from video...

I've gone over it a lot and can't make it happen. One issue that should be cleared up is that the Recipe tags aren't set properly and this causes confusing output. Tags variable is initialized as an array, but recipes with multiple tags are being set as strings:

<?php

//set as
$granola_muffins->addTag("breakfast, snack, quick bread");
//should be
$granola_muffins->addTag("breakfast", "snack", "quick bread");

Even after fixing this I still can't accomplish the same output as this video. Others mentioned that using array_key_exists instead of In_array should work, and it does work to an extent:

<?php

  public function getCombinedIngredients() {

    $ingredients = [];
    foreach($this->recipes as $recipe) {
        foreach($recipe->getIngredients() as $ing) {
          $item = $ing["item"];
          if(strpos($item, ",")) {
            $item = strstr($item, ",", true);       
       }

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

       $ingredients[$item] = array(
         $ing["amount"],
         $ing["measure"] 
       );
     }
   }             
        return $ingredients;

  } 

This almost achieves the same output as the instructor EXCEPT that she managed to somehow get rid of the singular version of the ingredients and keep the plural and I cannot do this with the same code.. This is confusing because isn't the code below assigning the value of the ingredient the singular version? If so, how is she getting the opposite result?

<?php

$item = substr($item, 0, -1);

I also am confused by this piece of code:

<?php

$ingredients[$item] = array(
         $ing["amount"],
         $ing["measure"] 
       );

I don't understand why we are setting setting $item as an index key with amount and measure as the values. We aren't using those values in the shopping list output?

The way I was best able to reproduce the output in the video is as follows:

<?php

 public function getCombinedIngredients() {

    $ingredients = [];
    foreach($this->recipes as $recipe) {
        foreach($recipe->getIngredients() as $ing) {
            $item = $ing["item"];

            if(strpos($item, ",")) {
                $item = strstr($item, ",", true);
            }
             $ingredients [] = $item;
          }
        }
        $ingredients = array_flip($ingredients);
        foreach($ingredients as $ing => $val) {
          if(isset($ingredients[$ing."s"])){
             unset($ingredients[$ing]); 
          }  
        }  

         return $ingredients;
     }

I really believe that the project files need to be better updated for each consecutive video and the instructor should be using those exact files for the demonstration.