芜湖不芜
你的代码在我这边不起作用,请尝试以下代码,它可以正常工作。<?phpheader('Content-Type: application/json');$parsed = json_decode(file_get_contents("construction.php"), true);$result = [];if($parsed['result']){ foreach($parsed['result'] as $key => $val){ foreach($val as $final_val){ $result[$key][] = [ "NM"=> $final_val['name'], "AG"=> $final_val['age'], "CA"=> $final_val['category'], "PL"=> $final_val['place'], "EY"=> $final_val['experience_yrs'] ]; } } // 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 = '{ "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" } ] }}';$parsed = json_decode($json, true);//print_r($parsed);foreach($parsed['result'] as $key => $val){ if($parsed['result'][$key]){ $subdata = $parsed['result'][$key]; foreach($subdata as $k1 => $v1){ $parsed['result'][$key][$k1] = [ "NM"=> $v1['name'], "AG"=> $v1['age'], "CA"=> $v1['category'], "PL"=> $v1['place'], "EY"=> $v1['experience_yrs'] ]; } }}echo json_encode($parsed);在这里你会得到{ "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" } ] }}