WEBVTT

00:00.740 --> 00:06.710
The for each loop has to be the simplest one to understand and to use.

00:07.580 --> 00:15.800
As the name might suggest, the idea is simple just go over every single item from the items array.

00:15.860 --> 00:24.320
Optionally also get the key, which might be the actual key from the associative array or an index,

00:25.550 --> 00:34.700
and get every single value and for every one of those values and optionally keys run the statements

00:34.700 --> 00:39.200
or the actual loop body and then you are done.

00:39.230 --> 00:43.910
So as a quick reminder we've seen arrays and the types of arrays.

00:43.940 --> 00:49.670
I haven't explained them in depth yet but we've used arrays before.

00:49.670 --> 00:57.680
So this is just this compound type where you have multiple items indexed by numbers starting with zero

00:57.710 --> 01:01.190
or indexed by keys.

01:01.640 --> 01:04.660
And they can also be Dimensional.

01:06.400 --> 01:09.910
So let's see an example of the for each loop.

01:10.660 --> 01:12.460
So let's see a quick example of a.

01:12.460 --> 01:14.680
For each loop let's create a.

01:14.710 --> 01:18.790
For each PHP file add the starting tag.

01:18.820 --> 01:22.840
And in this example we're going to use a basket.

01:23.080 --> 01:25.720
That would be an array associative array.

01:25.720 --> 01:29.230
So we've seen that on the diagram before there are keys.

01:29.230 --> 01:32.080
And keys have the values associated.

01:32.080 --> 01:36.310
So as keys I'm going to use the things that we can have in the basket.

01:36.310 --> 01:42.580
So I can have three apples and maybe four bananas.

01:42.580 --> 01:50.500
And we're going to use for each to just count every single item and maybe just display how many items

01:50.500 --> 01:52.750
of specific type someone has in a basket.

01:52.780 --> 01:58.540
Now also keep in mind that this symbol symbol here, this arrow that are actually those two characters

01:58.540 --> 02:01.540
placed together, it's just called ligature.

02:01.540 --> 02:05.680
And this is a thing that I have in my editor.

02:05.680 --> 02:11.010
So just remember those are two characters placed together.

02:11.250 --> 02:14.280
And now we're going to count the items.

02:14.280 --> 02:19.230
So let's use a variable called total initialized with zero.

02:19.230 --> 02:21.750
And now the for each loop.

02:21.780 --> 02:24.090
So we iterate over basket.

02:24.090 --> 02:26.730
So that's the array we're gonna go over.

02:26.760 --> 02:28.470
We use the as keyword.

02:28.470 --> 02:35.490
And first we type the name of the variable that will represent the current key.

02:35.520 --> 02:37.800
Then again this arrow symbol.

02:37.800 --> 02:43.380
And then we type the variable name that will represent the current value.

02:43.410 --> 02:47.520
So what this means is we're going to go over the basket array.

02:47.520 --> 02:55.590
And this body inside the loop will be run for every item, which in this case is the key.

02:55.590 --> 03:00.240
And for every quantity which is this value.

03:00.270 --> 03:03.510
Now keep in mind that the key is optional.

03:03.540 --> 03:10.470
This is also a valid for each loop where you're gonna skip the keys and only care about values.

03:10.470 --> 03:12.740
Now that's sometimes the case.

03:12.740 --> 03:15.500
In our case, we care about both.

03:15.710 --> 03:19.580
So let me just display the count of specific item.

03:19.580 --> 03:22.820
So I'm gonna do item equals.

03:25.340 --> 03:26.390
Quantity.

03:26.870 --> 03:30.650
Add a new line so the output is visible.

03:30.650 --> 03:36.860
And I need to remember about adding to the total quantity of items.

03:36.860 --> 03:38.990
So I can do it this way.

03:38.990 --> 03:49.910
Either reassign total as total plus quantity or use a shorthand which would be a plus equals, which

03:49.910 --> 03:54.590
means add the value on the right to the variable on the left.

03:54.860 --> 03:56.210
I'm going to use this shortcut.

03:56.210 --> 04:02.990
And finally we'll just say total items equals total.

04:03.020 --> 04:04.310
Add a new line.

04:04.310 --> 04:05.480
And that's it.

04:05.510 --> 04:09.470
Now let's run PHP for each PHP.

04:09.500 --> 04:10.190
There it is.

04:10.190 --> 04:15.410
We've got three apples, four bananas, and seven items in total.
