Here is my array in Json file
{
"0": { "type": 12, "index": 115, "showType": 1, "achievementType": 0, "desc": "WING" },
"4": { "type": 12, "index": 119, "showType": 1, "achievementType": 0, "desc": "WING" },
"1": { "type": 12, "index": 116, "showType": 1, "achievementType": 0, "desc": "WING" },
"3": { "type": 12, "index": 118, "showType": 1, "achievementType": 0, "desc": "WING" },
"2": { "type": 12, "index": 117, "showType": 1, "achievementType": 0, "desc": "WING" }
}
results to be achieved from php as json
{
"0": { "type": 12, "index": 115, "showType": 1, "achievementType": 0, "desc": "WING" },
"1": { "type": 12, "index": 116, "showType": 1, "achievementType": 0, "desc": "WING" },
"2": { "type": 12, "index": 117, "showType": 1, "achievementType": 0, "desc": "WING" },
"3": { "type": 12, "index": 118, "showType": 1, "achievementType": 0, "desc": "WING" },
"4": { "type": 12, "index": 119, "showType": 1, "achievementType": 0, "desc": "WING" }
}
CodePudding user response:
Convert to array, sort by index, then convert to obj.
PHP version
$json = '{
"0": { "type": 12, "index": 115, "showType": 1, "achievementType": 0, "desc": "WING" },
"4": { "type": 12, "index": 119, "showType": 1, "achievementType": 0, "desc": "WING" },
"1": { "type": 12, "index": 116, "showType": 1, "achievementType": 0, "desc": "WING" },
"3": { "type": 12, "index": 118, "showType": 1, "achievementType": 0, "desc": "WING" },
"2": { "type": 12, "index": 117, "showType": 1, "achievementType": 0, "desc": "WING" }
}';
$obj = json_decode($json, true);
usort($obj, function ($a, $b) {
if ($a["index"] > $b["index"]) {
return 1;
} else if ($a["index"] < $b["index"]) {
return -1;
} else {
return 0;
}
});
return $obj;
// or
return json_encode($obj);
JS version
var obj = {
"0": { "type": 12, "index": 115, "showType": 1, "achievementType": 0, "desc": "WING" },
"4": { "type": 12, "index": 119, "showType": 1, "achievementType": 0, "desc": "WING" },
"1": { "type": 12, "index": 116, "showType": 1, "achievementType": 0, "desc": "WING" },
"3": { "type": 12, "index": 118, "showType": 1, "achievementType": 0, "desc": "WING" },
"2": { "type": 12, "index": 117, "showType": 1, "achievementType": 0, "desc": "WING" }
}
function sort_obj_by_index(obj) {
var arr = [];
for (var key in obj) {
arr.push(obj[key]);
}
arr.sort(function(a, b) {
if (a.index > b.index) {
return 1
} else if (a.index < b.index) {
return -1
} else {
return 0
}
})
var result = {}
arr.forEach((value, key) => result[key] = value)
return result;
}
var result = sort_obj_by_index(obj)
console.log(result)
CodePudding user response:
$json = '{
"0": { "type": 12, "index": 115, "showType": 1, "achievementType": 0, "desc": "WING" },
"4": { "type": 12, "index": 119, "showType": 1, "achievementType": 0, "desc": "WING" },
"1": { "type": 12, "index": 116, "showType": 1, "achievementType": 0, "desc": "WING" },
"3": { "type": 12, "index": 118, "showType": 1, "achievementType": 0, "desc": "WING" },
"2": { "type": 12, "index": 117, "showType": 1, "achievementType": 0, "desc": "WING" }
}';
$obj = json_decode($json, true); // convert json to array
var_dump($obj); // print array before sorting
ksort($obj); // sorting
var_dump($obj); // print array after sorting
CodePudding user response:
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>example</title>
</head>
<body>
<?php
$filedata = file_get_contents('test.json');
$details = json_decode($filedata, true);
echo "<pre>";
ksort($details);
echo json_encode($details,
JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_FORCE_OBJECT);
echo "</pre>";
?>
</body>
</html>