[ACCEPTED]-PHP: Sort JSON data by keys-sorting

Accepted answer
Score: 11

ksort works with arrays, not with strings:

$array = json_decode($json, true);
ksort($array);
echo json_encode($array);  

0

Score: 2

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.

Score: 0

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

More Related questions