Sorting Arrays6:16 with Alena Holligan
What makes grouping variables in an array so useful? One of the extra tools arrays give us is the ability to sort. Sorting allows us to organize large amounts of data. We can sort by key or value, increasing the usefulness of our data.
PHP has several functions that deal with Sorting Arrays. The main differences are:
- Some sort based on the array keys, whereas others by the values: $array['key'] = 'value';
- Whether or not the correlation between the keys and values are maintained after the sort, which may mean the keys are reset numerically (0,1,2 ...)
- The order of the sort: alphabetical, low to high (ascending), high to low (descending), numerical, natural, random, or user defined
- Note: All of these sort functions act directly on the array variable itself, as opposed to returning a new sorted array
- If any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable).
Note that ksort will NOT work if numeric and string keys are mixed together, so let's comment out this line adding vanilla.
We've been looking at all the ways arrays let us group data and 0:00 store multiple details in one variable. 0:03 But what makes this so useful? 0:06 One of the extra tools that arrays give us is the ability to sort. 0:08 Sorting allows us to organize large amounts of data. 0:13 Take a library for example. 0:17 Books are usually organized by collection type like fiction or nonfiction. 0:19 And by subject, like arts and crafts or computer programming. 0:24 Books are then are arranged alphabetically by the author's last name. 0:28 If you're looking for The Hitchhiker's Guide To The Galaxy, 0:32 you would need to look under the last name Adams. 0:35 Some libraries sort fictional books even further by dividing out certain genres of 0:38 fiction, such as mystery, romance, westerns, and science fiction. 0:43 We can do the same thing with our arrays. 0:49 We can sort by key or value increasing the usefulness of our data. 0:51 Let's take a look at some of these options in relation to the arrays 0:56 we've created in this section. 1:01 In a browser let's search for 1:04 PHP sort arrays Click on the PHP sorting arrays manual. 1:06 We can see the many different options for sorting an array. 1:16 The first function we'll look at is asort. 1:19 Meaning array sort. 1:24 We see that we pass the function, the array we wish to sort. 1:26 Let's go back to workspaces in our indexed arrays.php file. 1:30 Here we have a simple list of items to learn. 1:36 Let's comment out this var_dump and 1:40 then let's add as asort right before our first echo. 1:43 We'll add a var_dump here. 1:51 And then run our script. 1:58 We see our array sorted by value without changing the key. 2:06 If I wanted the sort to actually re-index the array keys I could use 2:11 the sort function. 2:16 If I moved var_dump below my echo lines, 2:27 We see that the first item being arrays was removed first and 2:35 the last item being objects was removed next. 2:40 We could also change this to rsort, 2:43 Which will sort the values in reverse order. 2:52 This makes objects the first item, 2:56 removing it first then removing the last item which is now arrays. 2:59 We can also see that the rest of our items are in reverse order. 3:04 All these functions sort based on the elements value. 3:10 Let's comment out these display lines. 3:14 We can also sort randomly using the shuffle function. 3:23 This would allow me to grab a random item to learn. 3:31 Each time our script is run, a random item is pulled. 3:49 Besides sorting values, we can also sort by array key, 3:53 this is especially useful with associative arrays. 3:58 Back in the browser, let's go back to our list of sorts. 4:02 We see that we can use krsort and ksort to sort by key. 4:06 Let's go back to workspaces and 4:12 try this out with the favorite ice cream list in the associative_arrays.php. 4:14 Note that the ksort will not work if numeric and 4:20 string keys are mixed together. 4:24 So let's remove this adding of Vanilla. 4:27 Now before the var_dump, I want to add a ksort. 4:32 Ksort sorts my array alphabetically by key which is the person's name. 4:48 This allows me to easily find the person I'm looking for. 4:55 Notice the capital letters are sorted before lowercase letters. 4:59 I can also use the krsort. 5:03 This sorts keys in reverse order. 5:08 I can also sort associative keys by value. 5:11 Maybe I want to know who most enjoys chocolate ice cream. 5:16 When sorting associative arrays by value you'll most likely 5:24 want to use asort and arsort. 5:29 Using sort will re-index your associative array, removing your original 5:32 key value pair and thereby nullifying its usefulness. 5:37 Make sure you check the teacher's notes for 5:42 a chart on which sort functions maintain key association. 5:44 Congratulations on completing this exploration 5:50 of PHP's compound variable type arrays. 5:52 There are many other array functions available besides sorting. 5:56 And I encourage you to browse through the documentation link in the teacher's notes. 6:00 There are other fancy sorting options when it comes to sorting 6:05 multi-dimensional arrays. 6:09 I'll show you how to harness this and 6:10 other powerful features in the next section on loops. 6:12
You need to sign up for Treehouse in order to download course files.Sign up