0
$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);
}

This is returning:

array(1) {
    [0]=> object(stdClass)#2 (2) {
        ["id"]=> string(6) "coupon"
        ["amount"]=> float(9.99)
    }
}

I want to return just the amount of "9.99"

I tried $product_discount[0]['amount'], but that doesn't seem to be right??

3 Answers 3

6

It's an object, so you need the following syntax:

$product_discount = $mydata2->applied_discounts[0]->amount;

However, if you want an array instead, you could set json_decode()'s second parameter as TRUE:

$data2 = json_decode($json2, TRUE);
Sign up to request clarification or add additional context in comments.

Comments

3

You want to do this:

$product_discount = $mydata2->applied_discounts[0]->amount;

Comments

0

It's an object, not an array. Try...

$product_discount[0]->amount

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.