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

1
  • 2
    Usort : Returns TRUE on success or FALSE on failure. That's why $worth_array_final is TRUE.
    – Debflav
    Aug 20, 2014 at 12:02

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.