DEV Community

Sharmin Shanta
Sharmin Shanta

Posted on

1

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay