RockDance
RockDance

Reputation: 91

How arrange this array in ascending order by date?

this is my array

[comment] => Array
        (
            [0] => Array
                (
                    [mem_id] => 51
                    [comment] => nice...
                    [profilenam] => xyz
                    [photo_thumb] => photos/81951b37ad01c4aa65662956f178214eth.jpeg
                    [date] => 1307975661
                )

            [1] => Array
                (
                    [mem_id] => 329
                    [comment] => nice...
                    [profilenam] => abc
                    [photo_thumb] => photos/f841eab12f5a24ce12b984904760c05fth.jpeg
                    [date] => 1308043486
                )

        )

actually i wanted to arrange in ascending order by date , i used asort() but didn't work

Upvotes: 1

Views: 1851

Answers (3)

phihag
phihag

Reputation: 287755

usort($ar['comment'], function($v1, $v2) {
    return $v1['date'] - $v2['date'];
});

In php<5.3, use create_function instead of the anonymous function notation.

Upvotes: 5

Karolis
Karolis

Reputation: 9562

I haven't tested, but something like this:

uasort($comment, function ($a, $b) { return $a['date'] - $b['date']; } );

Upvotes: 0

LeleDumbo
LeleDumbo

Reputation: 9340

You are sorting array of array, in this case none of the built-in sort function with built-in comparison function would work. Try usort or uasort instead.

Upvotes: 1

Related Questions