Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Indexed Arrays 5:13
- Adding Array Elements 5:08
- List Quiz 5 questions
- Removing Array Elements 4:50
- Editing Array Elements 1:36
- Modifying Arrays 3 objectives
- Associative Arrays 3:48
- Associative Quiz 5 questions
- Mixing Data Types in Arrays 6:07
- Multidimensional Arrays 6:14
- Multidimensional Arrays 3 objectives
- Sorting Arrays 6:16
- Mixing and Sorting 5 questions
Well done!
You have completed PHP Arrays and Control Structures!

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
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.
Documentation
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).
See Also: sort, rsort, asort, arsort, ksort, krsort
Note that ksort will NOT work if numeric and string keys are mixed together, so let's comment out this line adding vanilla.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
edward wong
3,349 PointsWhy is 'Chocolate' at the top when it should be 'Black Cherry' when using asort()?
Posted by edward wongedward wong
3,349 Points1 Answer
-
Michael Apps
4,907 PointsAt frame 5:45 of the video why is the last value in the array "Black Cherry"
Posted by Michael AppsMichael Apps
4,907 Points2 Answers
-
Sam Gord
14,084 Points2 Answers
-
Andrew Patella
2,270 Points2 Answers
-
Antonia Senta
2,085 Points2 Answers
-
robbertdekock
13,390 Points6 Answers
-
Aaron Coursolle
18,014 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up