What if you want to sort by, say, title instead? Just put $titles first in the list:Īrray_multisort( $titles, $directors, $years ) Notice how array_multisort() has sorted the values in $directors in ascending order, then sorted the other 2 arrays so that the element order matches that of the now-sorted $directors array. $titles = array( "Full Metal Jacket", "Rear Window", "Mean Streets" ) Īrray_multisort( $directors, $titles, $years ) Īrray ( => Alfred Hitchcock => Martin Scorsese => Stanley Kubrick )Īrray ( => Rear Window => Mean Streets => Full Metal Jacket )Īrray ( => 1954 => 1973 => 1987 ) $directors = array( "Stanley Kubrick", "Alfred Hitchcock", "Martin Scorsese" ) To sort multiple arrays using array_multisort(), simply pass in all the arrays you want to sort. It can also sort multidimensional arrays, so it’s a pretty flexible function! Let’s take a look at sorting multiple arrays first, then we’ll explore sorting multidimensional arrays. The powerful array_multisort() function can sort multiple related arrays in one go, preserving the relationships between the arrays. Sorting multiple arrays and multidimensional arrays with array_multisort() Displays "Array ( => 1954 => Rear Window => 112 => Alfred Hitchcock )" Displays "Array ( => Alfred Hitchcock => 112 => Rear Window => 1954 )" Like asort() and arsort(), these functions preserve the relationships between keys and values. ksort() sorts the elements in ascending key order, while krsort() sorts in descending key order. Sorting associative arrays by key: ksort() and krsort()Īs well as sorting associative arrays by value, you can also sort them by key. Displays "Array ( => 1954 => 112 => Rear Window => Alfred Hitchcock )" To sort the values in ascending order, use asort():Īrsort() sorts the values in descending order - again while preserving the keys: These functions preserve not only the keys, but also the relationships between the keys and their values. If you want to sort the values in an associative array while preserving keys, use asort() and arsort() instead. Displays "Array ( => Alfred Hitchcock => Rear Window => 112 => 1954 )"Īs you can see, the sort() function has reindexed the array with numeric indices, destroying the original string indices of "title", "director", "year" and "minutes". $movie = array( "title" => "Rear Window", However, they can cause problems with associative arrays. Sort() and rsort() are fine for indexed arrays, where you don’t usually care about the relationship between keys and values. Sorting associative arrays: asort() and arsort() Rsort() works like sort(), except it sorts in descending order:Īll the sorting functions in this tutorial return true on success and false on failure. $myArray = array( 1, 2, 'a', 'b', 'A', 'B' ) įoreach ( $myArray as $val ) echo "$val " Uppercase letters come before lowercase letters, and all letters come before numbers. This means that A comes before Z, and 0 comes before 9. ![]() The sort() function sorts the values in an indexed array in ascending order. Sorting indexed arrays: sort() and rsort() Instead, you’ll learn about the functions that you’re likely to find the most useful in your everyday coding. We won’t try to cover every single PHP array-sorting function in this tutorial. ![]() These functions make it really easy to sort arrays without getting bogged down in the details of sort algorithms. For example, you can sort in ascending or descending order, by key, or by value. PHP gives you a large number of sorting functions that can sort arrays using many different criteria. One useful way you can manipulate this data is to sort it in a particular order. ![]() As I mentioned in my earlier article, Creating Arrays in PHP, arrays are very useful if you need to store and manipulate a large amount of data in one go.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |