1

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

hakre
  • 193,403
  • 52
  • 435
  • 836
RockDance
  • 91
  • 1
  • 1
  • 8
  • You're going to need `usort()` for that: http://www.php.net/manual/en/function.usort.php – PeeHaa Jun 14 '11 at 15:38
  • 2
    Seems like the comments are coming from the DB, why not sure 'ORDER BY date ASC' in the query getting these results? – Sabeen Malik Jun 14 '11 at 15:39
  • 1
    possible duplicate of [PHP sort multidimensional array by value](http://stackoverflow.com/questions/2699086/php-sort-multidimensional-array-by-value) – Gordon Jun 14 '11 at 15:41
  • @Sabeen: Out of curiosity, what makes you think these are coming from a database? – Evert Jun 14 '11 at 15:42
  • possible duplicate of [PHP Sort a multidimensional array by element containing date](http://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date) – Ferdinand Beyer Jun 14 '11 at 15:42
  • http://stackoverflow.com/search?q=sort+multidimensional+array+php – Gordon Jun 14 '11 at 15:42
  • @Sabeen Malik, thanks man i wasted 2-2:30 hrs to arrange this array using many way thanks a lot – RockDance Jun 14 '11 at 15:43
  • @Evert the hunch is mainly based on the way the array looks like. The ids and the date in the unix timestamp format suggests the same too. So just a suggestion in case that was the case :) – Sabeen Malik Jun 14 '11 at 15:44

3 Answers3

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

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

phihag
  • 278,196
  • 72
  • 453
  • 469
1

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.

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
0

I haven't tested, but something like this:

uasort($comment, function ($a, $b) { return $a['date'] - $b['date']; } );
Karolis
  • 9,396
  • 29
  • 38