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.
You need to sign up for Treehouse in order to download course files.Sign up