3

Currently I have some multidimensional arrays that look like this

Array ( 
    [71] => Array ( [author] => 2 [date] => 1392867376 ) 
    [49] => Array ( [author] => 2 [date] => 1392868188 ) 
    [75] => Array ( [author] => 14 [date] => 1392867388) 
    [67] => Array ( [author] => 2 [date] => 1392870805 ) 
)

I would like to sort them by the "date" but I have no idea how. I have tried this:

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

But since I have no idea, and could not find a reference on how to use the "comparison function" I'm up in space. All I've been able to find was very vague stuff. Currently this sorts by "author".

Can someone kindly explain to me how these comparison functions work (or point me to an online resource) and tell me what I need to do to sort this array by "date" - while keeping all keys intact (keys must not be changed or erased)

Many thanks for any help provided.

PS: I have tried array_multisort - It erased my keys.

Noodle Head
  • 401
  • 3
  • 10
  • 22
  • You can use array_multi_sort() function this will be helpfull – Sundar Feb 20 '14 at 05:04
  • possible duplicate of [How do I Sort a Multidimensional Array in PHP](http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php) – Satish Sharma Feb 20 '14 at 05:09
  • @Sundar I did try it and could not make it work. How would you work in a dynamic situation where the main keys are all random numbers, like my example above? The only one that worked without actually messing up my keys was uasort. Hence my question on it. Thanks. – Noodle Head Feb 20 '14 at 05:10
  • @SatishSharma Thanks but I actually tried that link and could not make it work at all. – Noodle Head Feb 20 '14 at 05:11
  • `var_dump($a, $b);` and see what they are equal to, not guess – zerkms Feb 20 '14 at 05:19
  • from php website " array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. Associative (string) keys will be maintained, but numeric keys will be re-indexed." So unfortunately this doesn't solve anything because as I said I need the keys to stay exactly as they are. – Noodle Head Feb 20 '14 at 05:21
  • @zerkms - this is what I get: array(2) { ["author"]=> int(14) ["date"]=> int(1392867388) } array(2) { ["author"]=> int(2) ["date"]=> int(1392867376) } – Noodle Head Feb 20 '14 at 05:26
  • @Noodle Head: and now you see your mistake? – zerkms Feb 20 '14 at 09:11

2 Answers2

6

try this cmp function:

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

It should work.

Maximus
  • 149
  • 6
0

Array date is sorted in ascending order

<?php
$a = array();
$a[71] = Array ('author' => 2, 'date' => 1392867376 );
$a[49] = Array ( 'author' => 14, 'date' => 1392868188 ) ;
$a[75] = Array ( 'author' => 2, 'date' => 1392867388) ;
$a[67] = Array ( 'author' => 2, 'date' => 1392870805 ) ;

$date = array();

// Obtain a list of columns
foreach ($a as $key => $row) {
    $date[$key]  = $row['date'];    
}

//sort the array date ascending order
array_multisort($date, SORT_ASC, $a);

//array is sorted in ascending order
print_r($a);
Sundar
  • 4,318
  • 6
  • 32
  • 56