1 00:00:00,310 --> 00:00:03,790 We've been looking at all the ways arrays let us group data and 2 00:00:03,790 --> 00:00:06,560 store multiple details in one variable. 3 00:00:06,560 --> 00:00:08,880 But what makes this so useful? 4 00:00:08,880 --> 00:00:13,520 One of the extra tools that arrays give us is the ability to sort. 5 00:00:13,520 --> 00:00:16,220 Sorting allows us to organize large amounts of data. 6 00:00:17,230 --> 00:00:19,230 Take a library for example. 7 00:00:19,230 --> 00:00:24,100 Books are usually organized by collection type like fiction or nonfiction. 8 00:00:24,100 --> 00:00:28,480 And by subject, like arts and crafts or computer programming. 9 00:00:28,480 --> 00:00:32,600 Books are then are arranged alphabetically by the author's last name. 10 00:00:32,600 --> 00:00:35,660 If you're looking for The Hitchhiker's Guide To The Galaxy, 11 00:00:35,660 --> 00:00:38,920 you would need to look under the last name Adams. 12 00:00:38,920 --> 00:00:43,720 Some libraries sort fictional books even further by dividing out certain genres of 13 00:00:43,720 --> 00:00:47,910 fiction, such as mystery, romance, westerns, and science fiction. 14 00:00:49,260 --> 00:00:51,790 We can do the same thing with our arrays. 15 00:00:51,790 --> 00:00:56,960 We can sort by key or value increasing the usefulness of our data. 16 00:00:56,960 --> 00:01:01,030 Let's take a look at some of these options in relation to the arrays 17 00:01:01,030 --> 00:01:02,670 we've created in this section. 18 00:01:04,030 --> 00:01:06,942 In a browser let's search for 19 00:01:06,942 --> 00:01:12,425 PHP sort arrays Click on the PHP sorting arrays manual. 20 00:01:16,446 --> 00:01:19,360 We can see the many different options for sorting an array. 21 00:01:19,360 --> 00:01:24,030 The first function we'll look at is asort. 22 00:01:24,030 --> 00:01:25,280 Meaning array sort. 23 00:01:26,480 --> 00:01:30,840 We see that we pass the function, the array we wish to sort. 24 00:01:30,840 --> 00:01:34,870 Let's go back to workspaces in our indexed arrays.php file. 25 00:01:36,340 --> 00:01:38,420 Here we have a simple list of items to learn. 26 00:01:40,640 --> 00:01:43,910 Let's comment out this var_dump and 27 00:01:43,910 --> 00:01:48,500 then let's add as asort right before our first echo. 28 00:01:51,990 --> 00:01:53,242 We'll add a var_dump here. 29 00:01:58,161 --> 00:01:59,270 And then run our script. 30 00:02:06,592 --> 00:02:11,350 We see our array sorted by value without changing the key. 31 00:02:11,350 --> 00:02:16,056 If I wanted the sort to actually re-index the array keys I could use 32 00:02:16,056 --> 00:02:17,494 the sort function. 33 00:02:27,271 --> 00:02:30,173 If I moved var_dump below my echo lines, 34 00:02:35,832 --> 00:02:40,015 We see that the first item being arrays was removed first and 35 00:02:40,015 --> 00:02:43,290 the last item being objects was removed next. 36 00:02:43,290 --> 00:02:47,124 We could also change this to rsort, 37 00:02:52,386 --> 00:02:54,570 Which will sort the values in reverse order. 38 00:02:56,590 --> 00:02:59,340 This makes objects the first item, 39 00:02:59,340 --> 00:03:04,590 removing it first then removing the last item which is now arrays. 40 00:03:04,590 --> 00:03:08,490 We can also see that the rest of our items are in reverse order. 41 00:03:10,020 --> 00:03:14,360 All these functions sort based on the elements value. 42 00:03:14,360 --> 00:03:16,207 Let's comment out these display lines. 43 00:03:23,131 --> 00:03:26,752 We can also sort randomly using the shuffle function. 44 00:03:31,949 --> 00:03:34,691 This would allow me to grab a random item to learn. 45 00:03:49,796 --> 00:03:53,825 Each time our script is run, a random item is pulled. 46 00:03:53,825 --> 00:03:58,135 Besides sorting values, we can also sort by array key, 47 00:03:58,135 --> 00:04:01,045 this is especially useful with associative arrays. 48 00:04:02,140 --> 00:04:04,880 Back in the browser, let's go back to our list of sorts. 49 00:04:06,280 --> 00:04:10,700 We see that we can use krsort and ksort to sort by key. 50 00:04:12,650 --> 00:04:14,320 Let's go back to workspaces and 51 00:04:14,320 --> 00:04:20,030 try this out with the favorite ice cream list in the associative_arrays.php. 52 00:04:20,030 --> 00:04:24,250 Note that the ksort will not work if numeric and 53 00:04:24,250 --> 00:04:26,090 string keys are mixed together. 54 00:04:27,500 --> 00:04:30,300 So let's remove this adding of Vanilla. 55 00:04:32,740 --> 00:04:35,841 Now before the var_dump, I want to add a ksort. 56 00:04:48,414 --> 00:04:53,660 Ksort sorts my array alphabetically by key which is the person's name. 57 00:04:55,230 --> 00:04:57,960 This allows me to easily find the person I'm looking for. 58 00:04:59,010 --> 00:05:02,390 Notice the capital letters are sorted before lowercase letters. 59 00:05:03,570 --> 00:05:05,605 I can also use the krsort. 60 00:05:08,360 --> 00:05:10,300 This sorts keys in reverse order. 61 00:05:11,800 --> 00:05:15,010 I can also sort associative keys by value. 62 00:05:16,260 --> 00:05:18,626 Maybe I want to know who most enjoys chocolate ice cream. 63 00:05:24,914 --> 00:05:29,885 When sorting associative arrays by value you'll most likely 64 00:05:29,885 --> 00:05:32,290 want to use asort and arsort. 65 00:05:32,290 --> 00:05:37,430 Using sort will re-index your associative array, removing your original 66 00:05:37,430 --> 00:05:41,569 key value pair and thereby nullifying its usefulness. 67 00:05:42,860 --> 00:05:44,850 Make sure you check the teacher's notes for 68 00:05:44,850 --> 00:05:48,300 a chart on which sort functions maintain key association. 69 00:05:50,050 --> 00:05:52,840 Congratulations on completing this exploration 70 00:05:52,840 --> 00:05:56,440 of PHP's compound variable type arrays. 71 00:05:56,440 --> 00:06:00,940 There are many other array functions available besides sorting. 72 00:06:00,940 --> 00:06:05,920 And I encourage you to browse through the documentation link in the teacher's notes. 73 00:06:05,920 --> 00:06:09,045 There are other fancy sorting options when it comes to sorting 74 00:06:09,045 --> 00:06:10,930 multi-dimensional arrays. 75 00:06:10,930 --> 00:06:12,930 I'll show you how to harness this and 76 00:06:12,930 --> 00:06:15,800 other powerful features in the next section on loops.