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" }
}
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)
$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
$filedata = file_get_contents('test.json');
$details = json_decode($filedata, true);
ksort($details);
Here 2 option
echo json_encode($details,
JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_FORCE_OBJECT);
// https://unminify.com/
OR
header('Content-type: text/javascript');
echo json_encode($details, JSON_PRETTY_PRINT);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.