Ad

Wednesday, May 07, 2008

Sorting Arrays in PHP…Some Sorting Functions

Some Sorting Functions in PHP

Oh, so you want to learn how to sort arrays in PHP. But, did I tell you that PHP has inbuilt sorting functions. Well, I’ve now! So this post will not be anything but discussion on those sorting functions. Let’s look at them without wasting any more time.

sort() Function

This function, as the name suggest can be used to sort single dimensional arrays in ascending order. PHP is intelligent enough to also sort string arrays very well besides numerical arrays.

$ar=array(1,11,38,65,2,99);
sort($ar);
//gives array(6) { [0]=> int(1) [1]=> int(11) [2]=>
//     int(38) [3]=> int(56) [4]=> int(65) [5]=> int(99) }


The optional second argument if specified can be SORT_REGULAR (default), SORT_NUMERIC or SORT_STRING. It can be used to explicitly tell the sort function the way you want the arrays to be sorted. Most commonly numerical arrays sorted with the SORT_STRING (second parameter) will give totally different results.

$ar=array(1,11,38,61,2,99);
sort($ar,SORT_STRING);
//gives array(6) { [0]=> int(1) [1]=> int(11) [2]=> 
//    int(2) [3]=> int(38) [4]=> int(61) [5]=> int(99) }

Sorting with this function is case-sensitive, capital case characters come before lowercase characters.

On sorting an array:

$ar=array('a','Z');
sort($ar);
//gives array(2) { [0]=> string(1) "Z" [1]=> string(1) "a" }

The sort() function has a matching function rsort(), which does sorting in reverse order.

asort() Function

When we have associative arrays having string indices (AKA keys), we cannot use the sort() function instead assort() function should be used.

This function sorts an array according to the values of the elements. It is same as the sort() function except that it can sort associative arrays.

$ar=array('jan'=>45,'feb'=>41,'mar'=>33);
asort($ar);
//gives array(3) { ["mar"]=> int(33) ["feb"]=> 
//    int(41) ["jan"]=> int(45) }

Again, its matching reverse sort function exists, arsort()

ksort() Function

It is also used for sorting associative arrays but in a different way. As you know associative arrays have two set of values, the ‘key’ and the ‘elements’. assort() function sorts the arrays according to the values of the elements. Guess what? This function sorts associative arrays according to the value of the ‘keys’ or string indices.

$ar=array('jan'=>45,'feb'=>41,'mar'=>33);
ksort($ar);
//gives array(3) { ["feb"]=> int(41) ["jan"]=> 
//    int(45) ["mar"]=> int(33) }

Its matching reverse sorting function is krsort() which does sorting in the same way but in the reverse order.

Previous Articles:

3 comments:

  1. Pankaj12:18 PM

    how are you Arvid. your post are excellent. I ave joined a course in web designing and your articles boost me a lot to learn more things.
    I really appreciate your afford.

    ReplyDelete
  2. @ Pankaj,
    I'm fine!

    And it's great to hear that.

    ReplyDelete
  3. I like this site because all the stuff which I look for are very well described.

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.