Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
I've saved the best for last! Foreach loops are my favorite when looping through arrays. You can use foreach loops with any type of array, and your array can have any key value. You can choose to grab just the item value or the key value as well. A foreach loop is a lot like combining the WHILE statement with the each function, only even easier.
DOWNLOAD FILE (list.php)
Documentation
foreach loops will allow you to grab the value and optionally the key, but not the key on its own.
I've saved the best for last.
0:00
foreach Loops are my favorite when
it comes to looping through arrays.
0:02
You can use foreach Loops
with any type of array and
0:06
your array can have any key value.
0:10
You can choose to grab just the item
value or the key value as well.
0:12
A foreach loop is a lot like combining
the while statement with the each function
0:17
only even easier.
0:22
Enough talk.
0:24
Let's start using a for each loop to loop
through our associative array of tasks.
0:24
I've included a file to download in
the teacher's notes for our list of items.
0:30
It's called list.php, and
0:34
it contains a multi-dimensional
array called list that is formatted
0:36
exactly like the multi-dimensional
array we created earlier.
0:41
I just added a few more items for
testing purposes.
0:46
Feel free to create your
own list of to do items.
0:49
>> We're going to be using the for
each loop extensively for our to do app.
0:53
So let's open up our todo.php file.
0:57
Instead of adding the array directly,
let's include the list.php file.
1:01
After downloading the file
from the teacher's notes,
1:07
you can upload the new file by right
clicking and choosing Upload File.
1:09
We then choose list.php.
1:15
And then choose Open.
1:20
Now we can remove this array and
include list.php.
1:24
The include works just as if we had
pasted the code from our list.php
1:32
file directly into this file.
1:37
But by using an include, we can keep
things organized and easier to read.
1:40
Now, that our list array is
available to loop through,
1:45
let's start our foreach loop.
1:48
Since, this is a foreach loop,
we use the keyword foreach.
1:52
We then add parentheses for
our evaluation, and curly braces for
1:56
the code we'll be
executing with each loop.
2:00
This is just like we've done for
the other loops.
2:05
Within the parentheses we tell
the loop which array we want to use.
2:08
We then use the keyword as, for
2:14
each item in our list we want to
work with it as the variable item.
2:17
This will assign one item from the list
to the variable item on each loop.
2:24
Item is now an associative array
with a named key and value.
2:30
Within the curly braces, we can add echo
2:34
$item title.
2:39
And then a line break.
2:44
Now let's preview this
file in the browser.
2:47
We see a list of item titles which is just
what we want, along with our var_dump.
2:51
We can get rid of that.
2:57
Now let's go back and display our items as
a table with all the associated values.
2:58
We'll remove this var_dump and
then we'll add our table.
3:04
We need the table tags to be on
the outside of our foreach loop because we
3:09
only want to add those once.
3:13
Let's also set up a header
row before the loop as well.
3:23
For each loop,
we're going to add a new row to the table.
4:05
So let's add our opening and
closing table row tags.
4:09
Now we can add table cell
tags around the title.
4:22
And we can change our line
break to end the table cell.
4:31
Let's duplicate the title row three
times for priority, due date and
4:36
completion status.
4:40
Now, let's refresh the browser again.
4:49
This time, we see a list of items in
a table with their priority, due date, and
4:52
completion status.
4:57
The completion status only
shows us 1 if complete,
4:59
which we know means true but
it's not very user friendly.
5:03
Let's add a conditional so we can change
the display of the complete status.
5:07
We'll put the opening and
closing table cells on their own line.
5:12
Then we'll add an if
statement between them.
5:21
If complete
5:23
Then we can echo yes.
5:31
Else we'll echo no.
5:36
Now let's refresh our browser again.
5:42
This time we see yes or no,
depending on the item status.
5:45
I told you we can also grab
the key along with the value.
5:50
So let's create a new foreach loop.
5:54
Let's add this new
foreach above the table.
5:57
Instead of just saying,
foreach loop as $item,
6:06
we're going to add a second
variable to represent the key.
6:10
Let's call this $key and
we add it before the item.
6:15
Then we use our double arrow,
= > to represent
6:19
the key value relationship just like we
used when assigning our associative array.
6:24
Now let's echo out our
key along with the item.
6:30
Now let's refresh our browser.
6:51
Now we can see the key along
with the title of each item.
6:53
>> Great job learning to
use the foreach loop.
6:58
I know that you'll find
it as useful as I do.
7:01
We have our to do app displaying our
list of items with all the details, but
7:04
I haven't shown you how to do some more
advanced sorting on our multi-dimensional
7:08
arrays and this list could really
use some help with sorting.
7:12
So in the next video,
I'm going to combine the things that
7:15
we've learned throughout this course to
make our to do app a lot more functional.
7:18
You need to sign up for Treehouse in order to download course files.
Sign up