-2

I'll try to sort a json format file by date and time

I try this method.

    function sortFunction( $a, $b ) {
        return strtotime($a["date"]) - strtotime($b["date"]);
    }

 $inp = file_get_contents('del.json');
$tempArray = json_decode($inp);
usort($tempArray, "sortFunction");
var_dump($tempArray);

del.json

[{"date":"2013-09-01 00:00:02","content":"1"},{"date":"2013-09-01 00:00:09","content":"5"},{"date":"2013-09-01 00:00:01","content":"3"}]

and ill get this error Cannot use object of type stdClass as array Thanks in advance!

I use this method and its work thanks to all your comment sorry i'm new bie hehe!

   function my_sort($a, $b)
    {
        if ($a->date < $b->date) {
            return -1;
        } else if ($a->date > $b->date) {
            return 1;
        } else {
            return 0; 
        }
    }

    usort($users, 'my_sort');
6
  • Your code is wrong here. PHP will not allow json-like direct declaration. Post your actual code, please. Commented Sep 9, 2013 at 8:30
  • I cannot reproduce it. As far as I know, your code triggers a parse error even in PHP 5.5. Is this your real code? Commented Sep 9, 2013 at 8:30
  • If you are getting json then use json_decode($data) before processing it Commented Sep 9, 2013 at 8:31
  • The array is malformed. AS @ÁlvaroG.Vicario said you should have a parse error. Take a look at error_reporting level Commented Sep 9, 2013 at 8:31
  • Whatever, I guess it's duplicate of Access JSON object name in PHP. Please have a look at it. Commented Sep 9, 2013 at 8:32

2 Answers 2

0

two changes will make sence:

function sortFunction( $a, $b ) {
    return strtotime($a->date) > strtotime($b->date) ? 1 : -1;
}

or

$tempArray = json_decode($inp, TRUE);
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you don't understand the sort callback.

It's return value increases or decreases the index of the actual element (by +1 or -1).

http://php.net/usort

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

The second thing is, your json array gets decoded with an numeric array which is sorted by default

$tempArray[0] = ...
$tempArray[1] = ...
$tempArray[2] = ...

Important for the sorting callback is the KEY, not the values of your array.

Steps to make it work:

  1. Decode the JSON array into PHP array

  2. Put the Date, converted to DateTime object into the key

    // Pseudo code:

    $tempArray[DateTime Object] = Array(values);

  3. use the usort with callback comparing the Date (>,<) instead of subtracting one by another (this is a semantical / logical error you have).

Tho, it would be easier to convert the Dates into unix timestamp and simply use natsort() on the array.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.