我收到数组格式:
[
{"value-1": "azerty", "value-2": "azerty"},
{"value-1": "azerty", "value-2": "azerty"}
]
,我希望将其转换为格式:
{
"value-1": ["azerty", "azerty"],
"value-2": ["azerty", "azerty"],
}
您将使用哪种方法在PHP中进行此转换?谢谢
答案 0 :(得分:2)
您可以像这样用foreach
收集所有密钥:
$data = json_decode($json,true);
$newArr = [];
foreach ($data as $item) {
foreach ($item as $key=>$value) {
$newArr[$key][] = $value;
}
}
echo json_encode($newArr);