Php 如何在json中获取对象的值以进行一些操作并使用新的对象值重建json?
我正在调用一个json数据,希望通过php对值进行更改,并返回带有新值的新json。 例如,这里是旧的json数据:Php 如何在json中获取对象的值以进行一些操作并使用新的对象值重建json?,php,json,Php,Json,我正在调用一个json数据,希望通过php对值进行更改,并返回带有新值的新json。 例如,这里是旧的json数据: [ { "key": 866, "price": "2.4" }, { "key": 867, "price": "4.3" } ] 并希望通过将“price”值乘以货币汇率来更改它,例如2,并使用新的price值重建json,如下所示: [ { "key":
[
{
"key": 866,
"price": "2.4"
},
{
"key": 867,
"price": "4.3"
}
]
并希望通过将“price”值乘以货币汇率来更改它,例如2
,并使用新的price值重建json,如下所示:
[
{
"key": 866,
"price": "4.8"
},
{
"key": 867,
"price": "8.6"
}
]
任何帮助都将不胜感激。谢谢。您可以输入、循环项目并完成工作,然后使用以下方式再次编码数据:
$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
}
]
您可以输入、循环项目并完成工作,然后使用以下方式再次编码数据:
$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
}
]
你试过什么?原来的和新的钥匙完全不同。你试过什么?原来的和新的钥匙完全不同。