views:

44

answers:

5

Ok I have the following array:

Array
(
   [0] => Array
    (
        [id] => 6
        [name] => This Course
        [time] => 1288082700
        [description] => blah blah . 
        [link] => http://this.com/?g=5
        [course] => 22
    )

[1] => Array
    (
        [id] => 2
        [name] => Workshop
        [time] => 1287561600
        [description] => This description
        [link] => http://this.com/?g=5
        [session] => 8
        [course] => 23
        [type] => standard
        [adobelink] => 
    )

)

How can I sort this entire array by using the inner 'time' key ?

Thanks!

+4  A: 

Use usort():

This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.

Example:

function cmp($a, $b) {
    if ($a['time'] == $b['time']) {
        return 0;
    }
    return ($a['time'] < $b['time']) ? -1 : 1;
}

usort($array, 'cmp');

Of course this will fail if an array has no time element. What should happen then depends on your requirements so I will leave the error handling to you ;)

Felix Kling
+1  A: 
http://dk.php.net/usort

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

usort($yourArray, 'sortByTime');
madsleejensen
would these functions work if there was more than 2 entries though?
Fearghal
Yes! as virtually all answers here are similar and it worked! Thanks guys!!
Fearghal
This functionn will work with large numbers of entries. PHP's usort calls the named function for every pair of values in the array.
Mark Baker
@Fearghal: Yes.
BoltClock
+3  A: 

Use PHP usort() function: http://php.net/manual/en/function.usort.php

First, define a function that will decide on compare result for your data structure:

function cmp($a, $b)
{
    if ($a['time'] == $b['time']) {
        return 0;
    }
    return ($a['time'] < $b['time']) ? -1 : 1;
}

Then call usort() and give the nmae of your function to it:

usort($array, "cmp");

You're done!

Silver Light
+2  A: 

uasort() will maintain your keys1.

uasort($a, function($a, $b) {
    $a = $a['time']; // Assuming keys exist
    $b = $b['time'];
    if ($a == $b) {
        return 0;
    } else {
        return $a < $b ? -1 : 1; // Reverse < if sort order is wrong
    }
});

Anonymous function syntax requires PHP 5.3+! Pass the name of the comparison function if <5.3 (see other answers).

1) In case you care about the keys, too. If not, just use the usort() approach found in abundance above :) The comparison function are basically identical (except for @codaddict's elegant approach).

jensgram
Maybe you should note that your code requires PHP 5.3.
Felix Kling
Adding to Felix's comment, specifically it's the anonymous function that requires PHP 5.3, and not `uasort()`.
BoltClock
@Felix Kling Good point. Edited.
jensgram
+4  A: 

You can use the usort function as:

function cmp($a,$b) {
        return $a['time'] - $b['time'];
}

usort($arr,'cmp');

Working link

codaddict
+1 for shortness (how embarrassing that I not thought about that) and link
Felix Kling
B-e-a-utiful. (15 chars)
jensgram