1 00:00:00,530 --> 00:00:03,630 Arrays are collection of items, which means that 2 00:00:03,630 --> 00:00:08,550 often times we want to do the same thing with each item in the array. 3 00:00:08,550 --> 00:00:10,980 Loops allow us to do just that. 4 00:00:10,980 --> 00:00:15,100 We can loop through an entire array, or part of an array, and 5 00:00:15,100 --> 00:00:18,630 perform any number of actions on each item. 6 00:00:18,630 --> 00:00:19,970 Let's go into work spaces and 7 00:00:19,970 --> 00:00:24,390 see how we can use a while loop to loop through each item on our learning list. 8 00:00:25,960 --> 00:00:32,242 Let's comment out this loop for years. 9 00:00:34,556 --> 00:00:36,955 Now let's open our indexed arrays file. 10 00:00:36,955 --> 00:00:44,710 We'll copy the code from our learned list along with the asort. 11 00:00:44,710 --> 00:00:47,570 Now we're ready to loop through each item. 12 00:00:47,570 --> 00:00:52,323 Let's pull up the documentation for the each function in PHP. 13 00:00:52,323 --> 00:00:56,826 PHP each function. 14 00:00:56,826 --> 00:01:00,664 We see that we pass it the array and it returns the current key and 15 00:01:00,664 --> 00:01:03,070 value pair from that array. 16 00:01:03,070 --> 00:01:05,210 And then it advances the array cursor. 17 00:01:06,500 --> 00:01:11,353 If we scroll down to example 2, We see 18 00:01:11,353 --> 00:01:16,530 that each is typically used in conjunction with list to traverse an array. 19 00:01:17,730 --> 00:01:23,410 If we look at list, we see that it's not really a function. 20 00:01:23,410 --> 00:01:29,170 But a language construct used to assign a list of variables in one operation. 21 00:01:30,490 --> 00:01:33,157 Let's go back to the example in the documentation for 22 00:01:33,157 --> 00:01:39,900 the Each function, And let's copy this while loop. 23 00:01:41,530 --> 00:01:45,060 Back in workspaces, we can paste in the loop and change our array. 24 00:01:47,620 --> 00:01:51,090 Now let's run the script in our console. 25 00:01:51,090 --> 00:01:54,893 PHP loops.php. 26 00:01:54,893 --> 00:01:57,680 We see our list sorted by value. 27 00:01:57,680 --> 00:02:03,438 We can also combine this with our count to only return a certain number of items, 28 00:02:03,438 --> 00:02:05,620 let's say 2. 29 00:02:05,620 --> 00:02:11,319 We'll add a variable named \$count, we'll set it equal to 0. 30 00:02:11,319 --> 00:02:17,968 And then we can add a condition to our loop, 31 00:02:17,968 --> 00:02:21,862 && \$count++ < 2. 32 00:02:21,862 --> 00:02:22,813 In the last video, 33 00:02:22,813 --> 00:02:26,840 we added our incrementing operator to the beginning of the variable. 34 00:02:26,840 --> 00:02:31,380 So that the number was incremented before the comparison was made. 35 00:02:31,380 --> 00:02:35,240 This time I want to compare first and then increment. 36 00:02:35,240 --> 00:02:39,415 So I add the incrementing operator to the end of the variable. 37 00:02:39,415 --> 00:02:44,460 This way the first time 0 will be evaluated as less than 2. 38 00:02:44,460 --> 00:02:49,920 And the second time, 1 will be evaluated as less than 2. 39 00:02:49,920 --> 00:02:50,950 Now let's run the script again. 40 00:02:53,800 --> 00:02:55,400 Now we see two empty items. 41 00:02:55,400 --> 00:02:56,470 Hey wait what went wrong? 42 00:02:57,650 --> 00:03:00,390 Remember when I talked about priority. 43 00:03:00,390 --> 00:03:04,140 The double ampersand has a higher priority than equal. 44 00:03:04,140 --> 00:03:09,290 So it's as if we put parentheses around the each and the count. 45 00:03:11,550 --> 00:03:13,450 That's not what we want to happen. 46 00:03:13,450 --> 00:03:14,420 So we have two options. 47 00:03:15,880 --> 00:03:20,730 We can change the double ampersand to the keyword AND or 48 00:03:20,730 --> 00:03:23,350 we can put parentheses around the first evaluation. 49 00:03:24,840 --> 00:03:28,090 I want to keep with the consistency of using double ampersands. 50 00:03:29,230 --> 00:03:33,028 So I'm going to put parentheses around this first evaluation. 51 00:03:41,580 --> 00:03:45,450 Now when I run the script, we see the first two items on the list. 52 00:03:45,450 --> 00:03:48,410 This could be a good way to see a couple items on your list 53 00:03:48,410 --> 00:03:49,650 without getting overwhelmed.