我如何获得数组形式文件json和排列索引值和结果返回数组json [WITH PHP]



这是我在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" }
}

从PHP获得的结果为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" }
}

先转换为数组,再按索引排序,再转换为obj。

PHP版本

$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);
<<p>

JS版本/strong>

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);

这里有2个选项

echo json_encode($details, 
JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_FORCE_OBJECT);
// https://unminify.com/

header('Content-type: text/javascript');
echo json_encode($details, JSON_PRETTY_PRINT);

最新更新