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

Dhruv Kapoor
Dhruv Kapoor
2,902 Points

When we use foreach loop to create $key=$item, how is $key storing numerical values and how are $item being initialised

So in the above video, we use foreach loop to display $key=$item, but the items are being initialised in next loop. Also, how is the value of $key is by default numerical that 0,1,2,3 when we have stored different keys in list.php

Dhruv Kapoor
Dhruv Kapoor
2,902 Points

<?php $list = array(); $list[] = array( 'title' => 'Laundry', 'priority' => 2, 'due' => '06/09/2016', 'day' => 0, 'repeat' => true, 'complete' => false, ); $list[] = array( 'title' => 'Dishes', 'priority' => 2, 'due' => null, 'day' => 0, 'repeat' => true, 'complete' => false, ); $list[] = array( 'title' => 'Dust', 'priority' => 3, 'due' => null, 'day' => 5, 'repeat' => true, 'complete' => false, ); $list[] = array( 'title' => 'Vacuum', 'priority' => 1, 'due' => '06/09/2016', 'day' => 1, 'repeat' => true, 'complete' => false, ); $list[] = array( 'title' => 'Make Dinner', 'priority' => 1, 'due' => null, 'day' => 0, 'repeat' => true, 'complete' => false, ); $list[] = array( 'title' => 'Clean Out Fridge', 'priority' => 2, 'due' => '07/30/2016', 'day' => 0, 'repeat' => true, 'complete' => true, ); ?>

<?php include 'list.php';

foreach($list as $key => $item){ //I AM TALKING ABOUT THIS LOOP echo $key. "=" . $item['title'] . "<br />\n"; } echo "<table>"; echo "<tr>"; echo "<th>Title</th>"; echo "<th>Priority</th>"; echo "<th>Due Date</th>"; echo "<th>Complete</th>"; echo "</tr>"; foreach($list as $item)//This will assign each element of array list to new array item { echo "<tr>"; echo "<td>" . $item['title'] . "</td>\n"; echo "<td>" . $item['priority'] . "</td>\n"; echo "<td>" . $item['due'] . "</td>\n" ; echo "<td>" ; if($item['complete']){ echo "Yes";} else{ echo "No";} echo "</td>\n" ; echo "</tr>"; } echo "</table>";

?>

Output

0=Laundry 1=Dishes 2=Dust 3=Vacuum 4=Make Dinner 5=Clean Out Fridge Title Priority Due Date Complete Laundry 2 06/09/2016 No Dishes 2 No Dust 3 No Vacuum 1 06/09/2016 No Make Dinner 1 No Clean Out Fridge 2 07/30/2016 Yes

1 Answer

The variable $item becomes an associative array when we use the foreach loop to it. So the items in the $list array gets transferred to the $item variable "as is". After which, we can then proceed to iterate on each element in the array to be shown in an html table.