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');