Sort array by value php
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending. Show
PHP - Sort Functions For ArraysIn this chapter, we will go through the following PHP array sort functions:
Sort Array in Ascending Order - sort()The following example sorts the elements of the $cars array in ascending alphabetical order: Example $cars = array("Volvo", "BMW", "Toyota"); Try it Yourself » The following example sorts the elements of the $numbers array in ascending numerical order: Sort Array in Descending Order - rsort()The following example sorts the elements of the $cars array in descending alphabetical order: Example $cars = array("Volvo", "BMW", "Toyota"); Try it Yourself » The following example sorts the elements of the $numbers array in descending numerical order: Sort Array (Ascending Order), According to Value - asort()The following example sorts an associative array in ascending order, according to the value: Example $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); Try it Yourself » Sort Array (Ascending Order), According to Key - ksort()The following example sorts an associative array in ascending order, according to the key: Example $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); Try it Yourself » Sort Array (Descending Order), According to Value - arsort()The following example sorts an associative array in descending order, according to the value: Example $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); Try it Yourself » Sort Array (Descending Order), According to Key - krsort()The following example sorts an associative array in descending order, according to the key: Example $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); Try it Yourself » Complete PHP Array ReferenceFor a complete reference of all array functions, go to our complete PHP Array Reference. The reference contains a brief description, and examples of use, for each function! PHP ExercisesPHP has several functions that deal with sorting arrays, and this document exists to help sort it all out. The main differences are:
"Matthew Rice" ¶ 9 years ago
oculiz at gmail dot com ¶ 11 years ago
Hayley Watson ¶ 5 years ago
How do you sort an array by a specific value in PHP?PHP Sorting Arrays. sort() - sort arrays in ascending order.. rsort() - sort arrays in descending order.. asort() - sort associative arrays in ascending order, according to the value.. ksort() - sort associative arrays in ascending order, according to the key.. How do you sort a value in an array?Array.prototype.sort() The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted.
What is Asort () and arsort ()?Definition and Usage
The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.
What does sort () do in PHP?Definition and Usage. The sort() function sorts an indexed array in ascending order. Tip: Use the rsort() function to sort an indexed array in descending order.
|