用PHP写出 然后使用 foreach 循环遍历json数组中的每一个订单,输出订单标题和订单金额。
时间: 2023-05-16 10:07:19 浏览: 106
<?php
$json = '{"orders":[{"title":"Order 1","amount":10},{"title":"Order 2","amount":20},{"title":"Order 3","amount":30}]}';
$data = json_decode($json, true);
foreach ($data['orders'] as $order) {
echo "Title: " . $order['title'] . ", Amount: " . $order['amount'] . "\n";
}
?>
输出结果为:
Title: Order 1, Amount: 10
Title: Order 2, Amount: 20
Title: Order 3, Amount: 30
相关问题
用PHP写出 然后使用 foreach 循环遍历json数组中的每一个订单,输出订单标题和订单金额。然后再封装json 数据输出
<?php
$json = '{
"orders": [
{
"title": "Order 1",
"amount": 10.99
},
{
"title": "Order 2",
"amount": 20.99
},
{
"title": "Order 3",
"amount": 30.99
}
]
}';
$data = json_decode($json, true);
foreach ($data['orders'] as $order) {
echo "Title: " . $order['title'] . ", Amount: " . $order['amount'] . "\n";
}
$output = json_encode($data);
echo $output;
?>
注意:以上代码是用 PHP 编写的,可以循环遍历 JSON 数组中的每一个订单,输出订单标题和订单金额,并封装 JSON 数据输出。
阅读全文