PHP - Usort returns 1 instead array [duplicate]
Asked Answered
R

1

7

I have multidimensional array that i want to sort by field containing unix timestamp:

 Array ( 
[0] => Array ( [0] => 723 [1] => 1442008738 ) 
[1] => Array ( [0] => 721 [1] => 1386802800 ) 
[2] => Array ( [0] => 718 [1] => 1356994800 ) 

) 

But when i use Usort, it just returns 1. What am i doing wrong?

function date_compare($a, $b)
{
    $t1 = $a[1];

    $t2 = $b[1];
    return $t1 - $t2;
}    
print_r(usort($dosortowania2, 'date_compare'));
Rubber answered 11/9, 2015 at 10:8 Comment(1)
RTFM - usort returns a Boolean true/false to indicate if successful or not, the array argument is pass by referenceRingworm
G
20

usort (http://php.net/usort) performs the sort directly on the provided array. The return value just returns boolean telling if the sorting suceeded.

usort($dosortowania2, 'date_compare');
print_r($dosortowania2);
Greasepaint answered 11/9, 2015 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.