0

I have a JSON with some data. One of the fields is a timestamp. Is there any way to sort the data based on the timestamp? Please don't recommend me any jQuery plugins like DataTables. And I don't want to fetch the data from the database in sorted order either. I use the following SQL command.

select * from tablename;

I don't want to get the data in sorted form from database by using a command something like this.

select * from tablename ORDER BY.....

Is it possible to sort JSON data like what I've said using PHP??? I want the data to be sorted in the descending order based on the timestamp. Any suggestions???

Here is a sample data

http://codepad.viper-7.com/TyLOWV

I tried this...

function sortByYear($a, $b) {
    $dA = new DateTime($a['date']);
    $dB = new DateTime($b['date']);

    return $dA->format('y') - $dB->format('y');
}


$d = json_decode($sample_data, true);
$info = $d['date'];

usort($info, 'sortByYear');

print_r($info);
6
  • Why is this a MySQL question? Commented Jun 26, 2014 at 5:59
  • Look at the question once again?? @shmosel Commented Jun 26, 2014 at 6:04
  • I see that you want to sort a JSON dataset, and you don't want to do it in MySQL. So what makes this a MySQL question? Commented Jun 26, 2014 at 6:07
  • Who said this is a mysql question??? @shmosel Commented Jun 26, 2014 at 6:09
  • It had a "mysql" tag. Commented Jun 26, 2014 at 6:10

1 Answer 1

2

Try with array_multisort function in PHP

$date = array();
$d = json_decode($sample_data, true);
foreach ($d as $key => $row)
{
    $date[$key] = $row['date'];
}
array_multisort($date, SORT_DESC, $d);
Sign up to request clarification or add additional context in comments.

2 Comments

So, the new sorted is stored in $date. Can I store in back to $d? @Sadikhasan
It will store in $d not in $date

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.