[ACCEPTED]-PHP: Sort JSON data by keys-sorting
Accepted answer
ksort works with arrays, not with strings:
$array = json_decode($json, true);
ksort($array);
echo json_encode($array);
0
In order to use ksort
, you first have to convert 3 the json to PHP array by using:
// the true argument specifies that it needs to be converted into a PHP array
$array = json_encode($your_json, true);
Then apply 2 ksort on that array.
And finally json_encode
it again to 1 get the result back in json.
this way here:
var dataArr = [];
for (value in oldData) {
var tmp = oldData[key];
dataArr.push(parseInt(key)tmp});
}
dataArr.sort(function(a, b){
if (a.word < b.word) return -1;
if (b.word < a.word) return 1;
return 0;
});
now in dataArr you have your sorted data
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.