所以,我正在接收 json 輸入。我不知道我會收到多少輸入。我知道如何將其轉換為數組,但是我需要混淆數組的結構。數組目前是多維的,并且已經是關聯(lián)的,但是使用了錯誤的關聯(lián)。對我來說,它使用“標簽”和“值”,但我需要將這兩個放在一個使用“x”和“y”的關聯(lián)數組中,以便我可以在圖表中使用它們。示例 json{ "metingen": [ { "label": "06-06-2019 13:13:38", "value": 25.21 }, { "label": "06-06-2019 13:51:04", "value": 27.69 }, { "label": "06-06-2019 13:52:04", "value": 27.69 }, { "label": "06-06-2019 13:53:06", "value": 27.61 }, { "label": "06-06-2019 13:54:08", "value": 27.56 }, { "label": "06-06-2019 13:55:08", "value": 27.55 }, { "label": "06-06-2019 13:56:09", "value": 27.55 }, { "label": "06-06-2019 13:57:09", "value": 27.53 }, { "label": "06-06-2019 14:05:12", "value": 28.51 }, { "label": "06-06-2019 14:06:12", "value": 28.53 }, { "label": "06-06-2019 14:07:13", "value": 28.51 }, { "label": "06-06-2019 14:08:13", "value": 28.51 }, { "label": "06-06-2019 14:09:14", "value": 28.53 }, { "label": "06-06-2019 14:10:14", "value": 28.52 }, { "label": "06-06-2019 14:11:15", "value": 28.52 }, { "label": "06-06-2019 14:12:15", "value": 28.54 }, { "label": "06-06-2019 14:13:16", "value": 28.53 }, { "label": "06-06-2019 14:14:16", "value": 28.48 }, { "label": "06-06-2019 14:15:17", "value": 28.39 }, { "label": "06-06-2019 14:16:17", "value": 28.37 } ], "title": "Temperature for ICT Boven"}
2 回答

弒天下
TA貢獻1818條經驗 獲得超8個贊
您可以將 array_map 與 array_combine 一起使用,如下所示,
$temp['metingen'] = array_map(function ($item) {
return array_combine(['x', 'y'], $item); // x and y are keys and label and value are values
}, $temp['metingen']);

慕容708150
TA貢獻1831條經驗 獲得超4個贊
你可以用foreach與json_decode
$jToArr = json_decode($json, true);
$res = [];
foreach($jToArr['metingen'] as $key => $value){
$res[] = ['x' => $value['label'], 'y' => $value['value']];
}
print_r($res);
- 2 回答
- 0 關注
- 122 瀏覽
添加回答
舉報
0/150
提交
取消