⊗ppPmAFSt 176 of 447 menu

Sorting Arrays in PHP

sort rsort ksort krsort asort arsort natsort natcasesort

Given an array:

<?php $arr = ['3'=>'a', '2'=>'d', '1'=>'c', '5'=>'e', '4'=>'b']; ?>

Sort the array in ascending order by values. This is what you should get:

['3'=>'a', '4'=>'b', '1'=>'c', '2'=>'d', '5'=>'e'];

Given an array:

<?php $arr = [10, 2, 35, 4, 15]; ?>

Sort the array in descending order by values. This is what you should get:

[35, 15, 10, 4, 2];

Given an associative array:

<?php $arr = ['z'=>'a', 'y'=>'d', 'x'=>'c', 'w'=>'b']; ?>

Sort the array in ascending order by keys. This is what you should get:

['w'=>'b', 'x'=>'c', 'y'=>'d', 'z'=>'a'];

Given an associative array:

<?php $arr = ['a'=>1, 'c'=>3, 'b'=>2, 'e'=>5, 'd'=>4]; ?>

Sort the array in descending order by keys. This is what you should get:

['e'=>5, 'd'=>4, 'c'=>3, 'b'=>2, 'a'=>1];

Given an array:

<?php $arr = ['a'=>5, 'b'=>2, 'c'=>4, 'd'=>1, 'e'=>3]; ?>

Sort the array in ascending order by values, preserving the keys. This is what you should get:

['d'=>1, 'b'=>2, 'e'=>3, 'c'=>4, 'a'=>5];

Given an array:

<?php $arr = ['a'=>10, 'b'=>40, 'c'=>20, 'd'=>30]; ?>

Sort the array in descending order by values, preserving the keys. This is what you should get:

['b'=>40, 'd'=>30, 'c'=>20, 'a'=>10];

Given an array:

<?php $arr = ['img12.png', 'img10.png', 'img2.png', 'img1.png']; ?>

Sort the array using "natural" sorting. This is what you should get:

['img1.png', 'img2.png', 'img10.png', 'img12.png'];

Given an array:

<?php $arr = ['IMG12.png', 'img10.png', 'img2.png', 'IMG1.png']; ?>

Sort the array using case-insensitive "natural" sorting. This is what you should get:

['IMG1.png', 'img2.png', 'img10.png', 'IMG12.png'];
byenru