我正在调用一个json数据,并希望通过php更改值并返回具有新值的新json。 例如,下面是旧的 JSON 数据:
[
{
"key": 866,
"price": "2.4"
},
{
"key": 867,
"price": "4.3"
}
]
并希望通过将"价格"值乘以货币汇率来更改"价格"值,例如2
并使用新的价格值重建 JSON,如下所示:
[
{
"key": 866,
"price": "4.8"
},
{
"key": 867,
"price": "8.6"
}
]
任何帮助将不胜感激。谢谢。
您可以json_decode
输入,循环项目并执行工作,然后使用json_encode
再次对数据进行编码,如下所示:
$data = json_decode($data, true); // Convert the JSON into an associative array
// Loop the items in the array
foreach($data as $key => $item) {
$data[$key]['price'] = $data[$key]['price'] * 1000; // Do your math here
}
echo json_encode($data); //Encode back to JSON
对于您问题中的输入将产生以下输出:
[
{
"key": 866,
"price": 2400
},
{
"key": 867,
"price": 4300
}
]