0

I am trying to form my array in order by date I have find my answer from this question and it is show the desire result through this answer but when I use print_r or json_encode it just show me true.

I don't know why?

The answer method:

function sortFunction( $a, $b ) {
    return strtotime($a["date"]) - strtotime($b["date"]);
}
usort($worth_array, "sortFunction");
echo "<pre>";
var_dump($worth_array);
echo "</pre>";

Result:

array(10) {
    [0]=>
      array(2) {
        ["date"]=>
          string(10) "2014-06-03"
        ["worth"]=>
          int(1131)
      }
    [1]=>
      array(2) {
        ["date"]=>
          string(10) "2014-06-04"
        ["worth"]=>
          int(4469)
      }
}

print_r method:

function sortFunction( $a, $b ) {
    return strtotime($a["date"]) - strtotime($b["date"]);
}
$worth_array_final = usort($worth_array, "sortFunction");
echo "<pre>";
print_r($worth_array_final);
echo "</pre>";

Result: 1

Community
  • 1
  • 1
xitas
  • 1,136
  • 3
  • 23
  • 47
  • 2
    Usort : _Returns TRUE on success or FALSE on failure._ That's why `$worth_array_final` is TRUE. – Debflav Aug 20 '14 at 12:02

1 Answers1

0

usort returns a boolean (see usort in php manual), and sorts the array passed as a reference. I.e. in your case $worth_array contains the sorted data and $worth_array_final only succes or not.

vollie
  • 1,005
  • 1
  • 11
  • 20