DEV Community

Sharmin Shanta
Sharmin Shanta

Posted on

Sort an array in PHP

$arr['one'] = 1; $arr['three'] = 3; $arr['four'] = 4; $arr['two'] = 2; $arr['five'] = 5; sort($arr); // sort the value as ascending order and return the new array: [0 => 1, 1 => 2, ...] rsort($arr); // sort the value as descending order and return the new array: [0 => 5, 1 => 4, ...] asort($arr); // sort the value as ascending order and return the array with the actual key: ['one' => 1, 'two => 2, ...] arsort($arr); // sort the value as descending order and return the array with the actual key: ['five' => 5, 'four => 4, ...] ksort($arr); // sort the key as ascending order and return the array with the actual value: ['five' => 5, 'four => 4, 'one' => 1, ...] krsort($arr); // sort the key as descending order and return the array with the actual value: ['two' => 2, 'three' => 3, 'one' => 1, ...] print_r($arr); 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)