Lavneet
Lavneet

Reputation: 626

How to extract value from a below json using php

Below is the starting of json that I've received in $json variable.

{"page":"0","cartaddonpopover":{"refreshfeature":1,"featurehtml":""},"initiateid":null,"containsmapitem":0,"wishlist":{"refreshfeature":1,"featurehtml":"\n<div id=\"cart-wishlist\" style=\"display:none;\" class=\"cart-wish-list\"  >\n</div>\n\n"},"gutter":{"refreshfeature":1,"featurehtml":"\n\n\n\n\n\n\n    \n    \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<div id=\"cart-gutter\" class=cart-gutter quantity=\"151\">\n\n  

I want to extract the value of quantity, which is 151 in above case.

I'm currently using, $quantity = $json->featurehtml->{"cart-gutter"}->quantity; and I know that I'm wrong. Please guide.

Upvotes: 0

Views: 110

Answers (3)

Barmar
Barmar

Reputation: 780724

Try this:

$html = str_get_html(json_decode($json)->featurehtml);
$quantity = $html->find("#cart-gutter")->quantity;

Upvotes: 3

Alexis
Alexis

Reputation: 66

You can always use a regexp to do this.

preg_match('/quantity=\\\"(\d+)\\\"/', $json, $matches);
$quantity = $matches[1];

Upvotes: 1

Abhishek
Abhishek

Reputation: 567

have you tried this.

$array = json_decode($json, true);
echo $array['quantity'];

Upvotes: -1

Related Questions