Multi dimensional array sorting in php

Posted by komals on Saturday, 2.02.13 @ 01:03am  •  Comments (2)

Sorting a multi dimensional array represents a very common and frequent requirement. Let me get straight to a practical example:

Here is an array of activities by several users on EduSagar:

$activities = array (
'1' => array(
'id' => 1,
'time' => "2013-01-22 22:20:00",
'text' => "Read an article"
'2' => array(
'id' => 2,
'time' => "2013-01-20 22:20:00",
'text' => "Wrote an article"
'3' => array(
'id' => 1,
'time' => "2013-01-24 22:20:00",
'text' => "Started a new discussion topic"

The idea here is to sort the multi dimensional array - activities based on the time field in the sub-array. Php provides a lot of functions for sorting single dimensional array and we will take help from those routines and figure out ways to achieve the task here.

Following is a routine that uses asort() function to sort a multi dimensional array by providing the array to be sorted and the index of the sub-array on which the sorting has to be performed.

function multid_sort($arr, $index) {
$b = array();
$c = array();
foreach ($arr as $key => $value) {
$b[$key] = $value[$index];


foreach ($b as $key => $value) {
$c[] = $arr[$key];

return $c;

$sorted_act = multid_sort($activities, 'time');

uasort() is another utility function provided by php library, which lets the user define their own comparison function. Following is an example to sort the array in descending order:

function act_cmp_function($a, $b) {
if ($a['time'] < $b['time']) {
return 1;
} else if ($a['time'] > $b['time']) {
return -1;
} else {
return 0;

uasort($activityfeed, 'act_cmp_function');

In case, your routines are there members of a class, the call to comparison function has to be modified as :

uasort($activityfeed, array($this, 'act_cmp_function'));

Although the above two methods are straight forward to follow, there is a little "tricky to understand but works well" alternative using array_multisort() :

function multid_sort($arr, $index) {
$temparr = array();
foreach ($arr as $key = > $value) {
$temparr[$key] = $value[$index];

array_multisort($temparr, SORT_DESC, $arr);

$sorted_act = multid_sort($activities, 'time');

array_multisort() can be used to sort several arrays at once. In this case temparr will be sorted in descending order and arr will be sorted based on the rearranged indexes from temparr. Here is a simple example of array_multisort() to demonstrate the concept (taken from the reference page for array_multisort from

$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);


ar1 is sorted in ascending order according to the integer values in the order - 0, 10, 100 and 100.

array(4) {
[0]=> int(0)
[1]=> int(10)
[2]=> int(100)
[3]=> int(100)

Now, ar2 will be rearranged based on the above sorting pattern i.e. element '4' will be first element as '0' is the first element in the sorted array ar1.

array(4) {
[0]=> int(4)
[1]=> int(1)
[2]=> int(2)
[3]=> int(3)

Hope this helps you in writing in your own sort function for multi dimensional array in php.

Discuss It !! (2 Comments)

Monday, 30.12.13 @ 02:00pm
php training kolkata
Excellent information,Hope to get more in future.<a href="">php training kolkata</a>
Thursday, 9.05.13 @ 12:17pm
Thanks for the article. Very useful.

Top 5 Users !!

Tweets by @edu_sagar