0

I have a url that returns a json. I want to loop through to get each of the values in the json.

This is what my json looks like.

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

My PHP code

    $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>";
    }
winfred adrah
  • 428
  • 6
  • 18

1 Answers1

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>";
}


?>
Musabbir Mamun
  • 251
  • 3
  • 6