【问题标题】:Loop through a json and get the values [duplicate]循环一个json并获取值[重复]
【发布时间】:2021-12-09 05:40:09
【问题描述】:

我有一个返回 json 的 url。我想循环获取json中的每个值。

这就是我的 json 的样子。

{"error":false,
"message":"Request orders successfully completed",
    "orders":[{
        "oid":505,
        "uid":234,
        "total_amount":"143.99000"
    },
        {
        "oid":506,
        "uid":234,
        "total_amount":"1.19000"
        }]
    }

我的 PHP 代码

    $getOrdersUrl = file_get_contents("https://apiurl?last_id=504");
    $json = json_decode($getOrdersUrl, true);
 if(!empty($json)){


        foreach($json as $val)
        {

            $oid = $val['orders'][0]['oid'];
            $uid = $val['orders'][0]['uid'];
            $total_amount = $val['total_amount'];
        
        echo $oid;

        }
    }else{
         echo "No Data to fetched to fetch for  <br>";
    }

【问题讨论】:

  • 你想要foreach($json['orders'] as $order),然后是$order['oid']等等

标签: php json foreach


【解决方案1】:
<?php 

$json = file_get_contents('https://api.jsonbin.io/b/6172d48d9548541c29c6ff05');
$data = json_decode($json, true);


foreach ($data['orders'] as $key => $value) {
    echo "oid = ".$value['oid']."<br>";
    echo "uid = ".$value['uid']."<br>";
    echo "total_amount = ".$value['total_amount']."<br>";
}


?>

【讨论】:

    猜你喜欢
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    相关资源
    最近更新 更多