php 循环取json中"orderTitle" :“1234”,取"orderTitle" 对应的键值 再用json循环输出
时间: 2023-05-14 15:04:41 浏览: 55
可以使用以下代码实现:
$json = '{"orders":[{"orderTitle":"1234","orderNumber":"5678"},{"orderTitle":"abcd","orderNumber":"efgh"}]}';
$data = json_decode($json, true);
foreach ($data['orders'] as $order) {
echo $order['orderTitle'] . "\n";
}
输出结果为:
1234
abcd
这段代码首先将 JSON 字符串解码为 PHP 数组,然后循环遍历数组中的每个订单,输出订单的标题。
相关问题
php 循环取json中"orderTitle" :“1234”,取"orderTitle" 对应的键值 再用json输出
可以使用以下代码实现:
<?php
$json = '{"orders":[{"orderTitle":"1234","orderNumber":"5678"},{"orderTitle":"abcd","orderNumber":"efgh"}]}';
$data = json_decode($json, true);
foreach ($data['orders'] as $order) {
if ($order['orderTitle'] === '1234') {
echo json_encode($order);
break;
}
}
?>
输出结果为:
{"orderTitle":"1234","orderNumber":"5678"}
php 循环取json中"orderTitle" :“1234”,取"orderTitle" 对应的键值
您可以使用以下代码来循环取出 JSON 中的 "orderTitle" 对应的键值:
$json = '{"orders":[{"orderTitle":"1234","orderDate":"2021-01-01"},{"orderTitle":"5678","orderDate":"2021-01-02"}]}';
$data = json_decode($json, true);
foreach ($data['orders'] as $order) {
echo $order['orderTitle'] . "\n";
}
这将输出:
1234
5678
请注意,这是 PHP 代码,您需要在 PHP 环境中运行它才能得到正确的结果。