需要在php中重命名json文件的所有Json键

我需要在 php.ini 中重命名 json 文件的所有键。


Json 文件:construction.php


{

  "result": {

    "UK": [

      {

        "name": "Leo Philips",

        "age": "28",

        "category": "Senior",

        "place": "London",

        "experience_yrs": "4"

      },

      {

        "name": "Mc Roy",

        "age": "25",

        "category": "Junior",

        "place": "London",

        "experience_yrs": "1"

      }

    ],

    "Europe": [

      {

        "name": "Reo Thomas",

        "age": "31",

        "category": "Senior",

        "place": "Paris",

        "experience_yrs": "6"

      }

    ]

  }

}

我想重命名所有键


“name”到 NA,“age”到 AG,“category”到 CA,“place”到 PL,“experience_yrs”到 EY


需要在 fullconstruction.php 中进行以下输出


{

  "result": {

    "UK": [

      {

        "NM": "Leo Philips",

        "AG": "28",

        "CA": "Senior",

        "PL": "London",

        "EY": "4"

      },

      {

        "NM": "Mc Roy",

        "AG": "25",

        "CA": "Junior",

        "PL": "London",

        "EY": "1"

      }

    ],

    "Europe": [

      {

        "NM": "Reo Thomas",

        "AG": "31",

        "CA": "Senior",

        "PL": "Paris",

        "EY": "6"

      }

    ]

  }

这是我在 fullconstruction.php 中使用的代码


<?php

header('Content-Type: application/json');


$parsed = json_decode(file_get_contents("construction.php"), true);

$result = [];

if($parsed['Success']){

    foreach($parsed['result'] as $val){

        $result[] = [

            "NM"=> $val['name'],

            "AG"=> $val['age'],

            "CA"=> $val['category'],

            "PL"=> $val['place'],

            "EY"=> $val['experience_yrs']

         ];

    }     // or what you want.


}


echo json_encode($result);

?>

得到空白 []。


怎么做?


小唯快跑啊
浏览 167回答 2
2回答

芜湖不芜

你的代码在我这边不起作用,请尝试以下代码,它可以正常工作。<?phpheader('Content-Type: application/json');$parsed = json_decode(file_get_contents("construction.php"), true);$result = [];if($parsed['result']){&nbsp; &nbsp; foreach($parsed['result'] as $key => $val){&nbsp; &nbsp; &nbsp; &nbsp;foreach($val as $final_val){&nbsp; &nbsp; &nbsp; &nbsp; $result[$key][] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "NM"=> $final_val['name'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "AG"=> $final_val['age'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "CA"=> $final_val['category'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "PL"=> $final_val['place'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "EY"=> $final_val['experience_yrs']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;// or what you want.}echo json_encode($result);?>输出{"UK":[{"NM":"Leo Philips","AG":"28","CA":"Senior","PL":"London","EY":"4"},{"NM":"Mc Roy","AG":"25","CA":"Junior","PL":"London","EY":"1"}],"Europe":[{"NM":"Reo Thomas","AG":"31","CA":"Senior","PL":"Paris","EY":"6"}]}希望这对你有用。

翻过高山走不出你

你必须循环两次使用下面的代码。$json = '{&nbsp; "result": {&nbsp; &nbsp; "UK": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "Leo Philips",&nbsp; &nbsp; &nbsp; &nbsp; "age": "28",&nbsp; &nbsp; &nbsp; &nbsp; "category": "Senior",&nbsp; &nbsp; &nbsp; &nbsp; "place": "London",&nbsp; &nbsp; &nbsp; &nbsp; "experience_yrs": "4"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "Mc Roy",&nbsp; &nbsp; &nbsp; &nbsp; "age": "25",&nbsp; &nbsp; &nbsp; &nbsp; "category": "Junior",&nbsp; &nbsp; &nbsp; &nbsp; "place": "London",&nbsp; &nbsp; &nbsp; &nbsp; "experience_yrs": "1"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ],&nbsp; &nbsp; "Europe": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "Reo Thomas",&nbsp; &nbsp; &nbsp; &nbsp; "age": "31",&nbsp; &nbsp; &nbsp; &nbsp; "category": "Senior",&nbsp; &nbsp; &nbsp; &nbsp; "place": "Paris",&nbsp; &nbsp; &nbsp; &nbsp; "experience_yrs": "6"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; }}';$parsed = json_decode($json, true);//print_r($parsed);foreach($parsed['result'] as $key => $val){&nbsp; &nbsp;if($parsed['result'][$key]){&nbsp; &nbsp; &nbsp; &nbsp;$subdata = $parsed['result'][$key];&nbsp; &nbsp; &nbsp; &nbsp;foreach($subdata as $k1 => $v1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $parsed['result'][$key][$k1] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "NM"=> $v1['name'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "AG"=> $v1['age'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "CA"=> $v1['category'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "PL"=> $v1['place'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "EY"=> $v1['experience_yrs']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}echo json_encode($parsed);在这里你会得到{&nbsp; "result": {&nbsp; &nbsp; "UK": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "NM": "Leo Philips",&nbsp; &nbsp; &nbsp; &nbsp; "AG": "28",&nbsp; &nbsp; &nbsp; &nbsp; "CA": "Senior",&nbsp; &nbsp; &nbsp; &nbsp; "PL": "London",&nbsp; &nbsp; &nbsp; &nbsp; "EY": "4"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "NM": "Mc Roy",&nbsp; &nbsp; &nbsp; &nbsp; "AG": "25",&nbsp; &nbsp; &nbsp; &nbsp; "CA": "Junior",&nbsp; &nbsp; &nbsp; &nbsp; "PL": "London",&nbsp; &nbsp; &nbsp; &nbsp; "EY": "1"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ],&nbsp; &nbsp; "Europe": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "NM": "Reo Thomas",&nbsp; &nbsp; &nbsp; &nbsp; "AG": "31",&nbsp; &nbsp; &nbsp; &nbsp; "CA": "Senior",&nbsp; &nbsp; &nbsp; &nbsp; "PL": "Paris",&nbsp; &nbsp; &nbsp; &nbsp; "EY": "6"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP