简体   繁体   English

如何获取数组表单文件 json 并安排索引值和结果返回数组 json [WITH PHP]

[英]How can I get array form file json and arrage Index value and result return array json [WITH PHP]

Here is my array in Json file这是我在 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" }
}

results to be achieved from php as json结果从 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" }
}

Convert to array, sort by index, then convert to obj.转换为数组,按索引排序,然后转换为 obj。

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

JS version JS版本

 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这里有2个选项

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM