views:

127

answers:

2

I have an array such as:

Array
(
[0] => Array
    (
        [id] => 2
        [type] => comment
        [text] => hey
        [datetime] => 2010-05-15 11:29:45
    )

[1] => Array
    (
        [id] => 3
        [type] => status
        [text] => oi
        [datetime] => 2010-05-26 15:59:53
    )

[2] => Array
    (
        [id] => 4
        [type] => status
        [text] => yeww
        [datetime] => 2010-05-26 16:04:24
    )

)

Can anyone suggest a way to sort/order this based on the datetime element?

+3  A: 

Use usort() and a custom comparison function:

function date_compare($a, $b)
{
    $t1 = strtotime($a['datetime']);
    $t2 = strtotime($b['datetime']);
    return $t1 - $t2;
}    
usort($array, 'date_compare');

EDIT: Your data is organized in an array of arrays. To better distinguish those, let's call the inner arrays (data) records, so that your data really is an array of records.

usort will pass two of these records to the given comparison function date_compare() at a a time. date_compare then extracts the "datetime" field of each record as a UNIX timestamp (an integer), and returns the difference, so that the result will be 0 if both dates are equal, a positive number if the first one ($a) is larger or a negative value if the second argument ($b) is larger. usort() uses this information to sort the array.

Ferdinand Beyer
in this example, does $array need to contain elements $a and $b? - im getting an error invalid compare function
Tim
Nope, `$a` and `$b` will hold the arrays within `$array`. Make sure that you did not misspell the function name.
Ferdinand Beyer
im not sure I understand. In my example, I have 3 arrays in the array - I have confirmed the name, still getting invalid comparison function
Tim
See the explanation I added. I tested the code and it works fine for me!
Ferdinand Beyer
Thanks, your explanation made sense.Im still getting the error. Im using Codeigniter framework and have added the date_compare function to the controller. ive checked the spelling, tried alternate spelling, I tried just returning 0 from the function, still getting the error
Tim
I don't know CodeIgniter, but are you defining the function inside a class? Than you either have to define it outside the class or use a different callback argument: `usort($array, array($this, "date_compare"))`, so that `usort()` knows it's a class function/method. See also: http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback
Ferdinand Beyer
A: 

http://us2.php.net/manual/en/function.array-multisort.php see third example:

<?php

$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);

foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

?>

fyi, using a unix (seconds from 1970) or mysql timestamp (YmdHis - 20100526014500) would be be easier for the parser but i think in your case it makes no difference.

Tobias