Sort php array by key
(PHP 4, PHP 5, PHP 7, PHP 8) Show ksort — Sort an array by key in ascending order Descriptionksort(array
Parametersarray The input array. flags The optional second parameter Sorting type flags:
Return Values Always returns ExamplesExample #1 ksort() example
The above example will output: a = orange b = banana c = apple d = lemon Example #2 ksort() with int keys
The above example will output: array(3) { [0]=> string(5) "First" [2]=> string(4) "Last" [1]=> string(6) "Middle" } array(3) { [0]=> string(5) "First" [1]=> string(6) "Middle" [2]=> string(4) "Last" } See Also
DavidG ¶ 12 years ago
$people as $person){
orlov0562 at gmail dot com ¶ 5 years ago
thegrandoverseer ¶ 10 years ago
Anonymous ¶ 20 years ago
ssb45 at cornell dot edu ¶ 17 years ago
serpro at gmail dot com ¶ 13 years ago
count($array))
bimal at sanjaal dot com ¶ 8 years ago
$domains as $d => $domain) sbarnum at mac dot com ¶ 20 years ago
Anonymous ¶ 9 years ago
delvach at mail dot com ¶ 20 years ago
justin at booleangate dot org ¶ 17 years ago
Anonymous ¶ 8 years ago
stephen [ at ] brooksie-net [ dot ] co [ dot ] uk ¶ 9 years ago
jakub dot lopuszanski at nasza-klasa dot pl ¶ 11 years ago
maik dot riechert at animey dot net ¶ 14 years ago
centraleffects at yahoo dot com ¶ 9 years ago
How do I sort an array by key?The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key. Tip: Use the asort() function to sort an associative array in ascending order, according to the value.
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 an array of arrays in PHP?To PHP sort array by key, you should use ksort() (for ascending order) or krsort() (for descending order). To PHP sort array by value, you will need functions asort() and arsort() (for ascending and descending orders).
How do you sort an array of objects in PHP?Approach: The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field.
|