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!

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 PHP Arrays and Control Structures PHP Loops Todo App

Alex Forseth
Alex Forseth
8,017 Points

Why does the $order array display a value of 5 when there 5 false's?

So the below code somehow becomes presented as a value of 5 when alena var_dumps $order. My question is how does it equal 5 when there is only a value of 1 $item['complete'] as in 'complete' => false.

Any explanation for this would be greatly appreciated.

$order = array(); 
foreach ($list as $key => $item) {
 if ($item['complete']) {
   $order[] = $key;
john paul
john paul
3,191 Points

Hi, I wondered this too... but then I realised that the 5 denoted array 5 in $list that hold the vlaue 'complete' , so the 5 is only referencing the array number where it found the statement to be true.

1 Answer

To begin with, I believe that you may be misunderstanding the conditional statement:

if ($item['complete'])

This is checking to see if there is an element ($item) that has a value with a key of 'complete' that evaluates to true. If you used the negation operator (!) in the conditional, then it would be checking to see if the item had a value with a key of 'complete' that evaluates to false. Like this:

if (!$item['complete'])

If you var_dump the $list array as made available for download in the tutorial, you'll see that there is only one element where: ["complete"]=>

That element is: ["title"]=>
string(16) "Clean Out Fridge"

This is the sixth element in $list, and therefore (zero index) it has a key of 5. When you var_dump $order, there is only one element in the array, with the integer value of 5. This value is pulled from the key of the element ("Clean out fridge") and then added to $order in the foreach loop.

Here's an explanation on what's happening there:

the foreach is looping through the $list array using this syntax (see the foreach documentation from the PHP Manual) :

foreach (array_expression as $key => $value)

In the referenced code snippet, this is written:

foreach ($list as $key => $item)

On each iteration, the value of the current element is assigned to $item and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element). Additionally, the current element's key is being assigned to the $key variable on each iteration. So, on the first loop, the value of the $key variable would be set to 0. Then, on the next loop it would be set to 1, and so on.

As mentioned previously, when the foreach loops, the conditional statement is checking to see if the current element ($item) has a key named 'complete' that evaluates to the boolean 'true'. If the conditional statement evaluates to true, then the current $key variable will be added to the $order array.

if ($item['complete']) {
   $order[] = $key;

So, when the conditional statement evaluates the only element with a 'complete' key that has a true value, the key (5) of that element gets added as an integer value to the $order array.

My apologies for the long-winded explanation, I hope that it's correct and is helpful. I'm just learning PHP myself (my background is in JavaScript), so a lot of these concepts are new to me as well.