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

Alvin Hue
Alvin Hue
4,496 Points

Don't get what it means?

Don't quite understand what the code below is actually doing, I don't see any "amount" and "measure" in the output.

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

1 Answer

Simon Coates
Simon Coates
28,694 Points

Here's a demo so you can see where the values come from and where they end up:

<?php
$ingredients = [];

//each iteration of the loops, assigns a ingredient to $ing.
//The following simulates one iteration through the loop. An associated array is assigned to $ing
$ing = [
    'item'=>"chocolate",
    'amount'=>'2',
    'measure'=>'cups'
    ];
//the values of ing are used to create an entry in the ingredients array.
$ingredients[$ing["item"]] = array( $ing["amount"], $ing["measure"] );

var_dump($ingredients);

produces:

array(1) {
  ["chocolate"]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(4) "cups"
  }
}