$url2 = "http://www.website.com/test.json";
$json2 = file_get_contents($url2);
$data2 = json_decode($json2);
foreach($data2 as $mydata2) {
$product_discount = $mydata2->applied_discounts;
var_dump($product_discount);
}
Run Code Online (Sandbox Code Playgroud)
这是回归:
array(1) {
[0]=> object(stdClass)#2 (2) {
["id"]=> string(6) "coupon"
["amount"]=> float(9.99)
}
}
Run Code Online (Sandbox Code Playgroud)
我想只返回"9.99"的金额
我试过$ product_discount [0] ['amount'],但这似乎不对?
它是一个对象,因此您需要以下语法:
$product_discount = $mydata2->applied_discounts[0]->amount;
Run Code Online (Sandbox Code Playgroud)
但是,如果您想要一个数组,可以将json_decode()第二个参数设置为TRUE:
$data2 = json_decode($json2, TRUE);
Run Code Online (Sandbox Code Playgroud)