1

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!

5 Answers 5

5

You can use the usort function as:

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

usort($arr,'cmp');

Working link

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for shortness (how embarrassing that I not thought about that) and link
4

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 ;)

Comments

3

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!

Comments

2

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).

1 Comment

Adding to Felix's comment, specifically it's the anonymous function that requires PHP 5.3, and not uasort().
1
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');

3 Comments

would these functions work if there was more than 2 entries though?
Yes! as virtually all answers here are similar and it worked! Thanks guys!!
This functionn will work with large numbers of entries. PHP's usort calls the named function for every pair of values in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.