From the course: Functional Programming with PHP

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Sorting

Sorting - PHP Tutorial

From the course: Functional Programming with PHP

Start my 1-month free trial

Sorting

- [Instructor] Now let's look at another important operation when working with arrays, sorting. Now to do sorting, we'll be using another one of PHP's built-in array functions, a function called, as you may have guessed, sort. This function is useful when we want to change the order of elements in an array. This would be very useful, for example, if we add in array of names and we wanted to sort it in alphabetical order, or if we simply wanted to sort an array of numbers. Before we look at how to use sort, it's very important to keep in mind that sort is a mutating function which means that the array that we pass to sort is passed by reference instead of by value, and actually gets modified. And that's just something to keep in mind since that interferes with the functional theme of immutability. So how do we use sort? Sort takes an array as an argument, as well as one or more sort flags that specify how the elements…

Contents