Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Sorting Arrays.docx59 KB Sorting Arrays - Solution.docx
59.2 KB
Quick reference
Sorting Arrays
In this video we'll learn how to sort arrays and count objects in an array.
When to use
You often need to know how many items are in an array, and sometimes need to sort items in an array.
Instructions
To count the number of items in an array:
echo count($array_name);
To sort an indexed array in ascending order
sort($array_name);
To sort an indexed array in descending order
rsort($array_name);
To sort an associative array in ascending order (use a foreach loop)
asort($array_name);
To sort an associative array in descending order (use a foreach loop)
rsort($array_name);
To print out the items in an associative array, use a foreach loop:
foreach ($array_name as $y){
echo $y . "<br/>";
}
Hints & tips
- Counting the number of items in an array is useful. echo count($array_name);
- sort(); ascending
- rsort(); descending
- to print out an associative array use a foreach loop
- 00:04 In this video, I wanna talk about sorting arrays and also counting arrays.
- 00:09 So let talk about counting first, now in this array we have three items, and
- 00:13 we know three items cuz we just created it.
- 00:15 There's lot of times when you may not know, how many items are in an array for
- 00:18 a lot of different reasons.
- 00:20 You may have created the array programatically,
- 00:22 there maybe hundreds of thousands you may have lost track,
- 00:25 whatever the reason you need to know how many things are in there.
- 00:28 And that's very easy to do in fact we can just echo out and
- 00:31 what we do is use something called count, and
- 00:34 then inside of here we just type in the name of our array.
- 00:38 So if we save this click reload we see three.
- 00:42 So very easy.
- 00:43 Now next I wanna talk about sorting arrays.
- 00:45 Before we do that, let's print out our entire array to the screen, and
- 00:50 we'll use a while loop for that.
- 00:52 Ad I'm just gonna print this out, it's very simple here.
- 00:55 It's just basic while loop, we set our counter at 0 and
- 00:59 now look at this, while our our counter is less than our array count.
- 01:03 In this case, three.
- 01:05 While 0 < 3, just echo out the item.
- 01:10 And you could see right here instead of putting the index number for
- 01:13 array, I'm using the count and then we go ahead and increment it.
- 01:16 And I put a line break also so that it's not all on one line.
- 01:19 So let's go ahead and look at this and hit reload.
- 01:22 So pepperoni, cheese, mushroom.
- 01:24 Pepperoni, cheese, mushroom, okay.
- 01:25 So we're printing out to the screen.
- 01:27 So now let's talk about sorting.
- 01:29 There's a couple of ways to sort indexed arrays, and
- 01:32 a couple of ways to sort associative arrays.
- 01:34 And let's look at index first.
- 01:36 Here we're echoing out our pizza but
- 01:38 instead of doing that, first of all we can sort this fairly easily.
- 01:42 You can sort in ascending or descending, and ascending means up,
- 01:46 descending means down.
- 01:48 In this case it's gonna be alphabetically because these are strings, but
- 01:51 if they were numbers it would be ascending, would be 1, 2, 3, 4, 5, 6,
- 01:54 7, 8, 9, 10, descending would be 10, 9, 8, 7, 6, 5, 4,3, 2,1.
- 01:58 To do that, we just go sort and
- 02:01 then we put the name of our array that we want to sort.
- 02:05 So if we save this, come back here, here's how it was, pepperoni, cheese, mushrooms.
- 02:09 Now we hit reload, cheese, mushroom, pepperoni, and
- 02:12 you can see this is ascending alphabetically, C, M, P, alphabetically.
- 02:16 Likewise, we can do descending just by doing rsort.
- 02:21 So if we save this and hit reload, pepperoni, mushroom, cheese, P, M and C,
- 02:25 that's sort of alphabetically descending.
- 02:27 And if you wanna see this better we can change these to numbers.
- 02:31 So we go 3, 6, 23, 100 and 0, so
- 02:35 now it's gonna sort these in descending order from biggest to least,
- 02:41 or ascending order if we change this to sort from least to greatest.
- 02:48 So that's how you sort an indexed array.
- 02:50 We can do the same sort of thing here, I'm just gonna delete this and
- 02:54 come down here to sort an associative array it's a little bit different.
- 02:59 We're gonna use something called a for each loop, it's like a for loop but
- 03:02 it's specifically for associative arrays.
- 03:05 And it just looks like this.
- 03:06 For each age, we call it an array, as a y.
- 03:09 And what that's doing is, it's taking these values and
- 03:12 putting it into the y variable.
- 03:14 We could just echo out that number.
- 03:17 So if we save this, come up here and hit reload 39, 42, 29.
- 03:22 39, 42, 29.
- 03:24 That's just printing it out.
- 03:26 Now if we want to sort this array, we could do rsort and asort.
- 03:30 Rsort looks just like this and we pop in our age, and
- 03:35 if we save this rsort is descending.
- 03:38 So if we hit reload from high to low.
- 03:41 Likewise, asort.
- 03:44 Is ascending.
- 03:46 So hit reload.
- 03:47 Low to high.
- 03:49 Those are the two main ways to sort arrays.
- 03:52 Two ways for index and two ways for associative arrays.
- 03:56 And you can see we learned a little bit more about loops and
- 03:58 stuff in this video as well.
- 03:59 But you use these sometimes, but really not all that often.
- 04:02 Just sort of a useful thing to know.
- 04:04 So in the next video, we will look at functions.
Lesson notes are only available for subscribers.