Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Arrays are a collection of items, which mean that, often times, we want to do the same thing with each item in the array. Loops allow us to do just that. We can loop through an entire array, or part of an array, and perform any number of actions on each item. In this video we'll use our while loop to loop through the todo list.
Documentation
PLEASE NOTE: This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged. However, you may still encounter the each function both in PHP and other languages, so knowing how it works is still important.
each: returns the current key and value pair from an array and advance the array cursor.
list: assigns variables as if they were an array.
Additional Examples
//increment before evaluating the $count variable
$count=0;
while ((list($key, $val) = each($todo)) && (++$count <= 2)) {
echo "$key => $val\n";
}
//increment after evaluating the $count variable
$count=1;
while ((list($key, $val) = each($todo)) && ($count++ <= 2)) {
echo "$key => $val\n";
}
The above examples will both return the second and third elements (array keys 1 and 2).
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up