簡體   English   中英

如何獲取數組表單文件 json 並安排索引值和結果返回數組 json [WITH PHP]

[英]How can I get array form file json and arrage Index value and result return array 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);

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

這里有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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM