ajay87
ajay87

Reputation: 17

Searching JSON with PHP

I grab a load of JSON data which i decode using php json_decode. I want to search or extract certain elements of the JSON object. I've looked at a few similar examples with little success.

The JSON structure is like this. I want to extract the stock element to compare, using PHP.

{
"items":{
         "item000":{
            "color":"black",
            "skuId":"sku000",
            "price":{
               "sku000":"139.99"
            },
            "name":"item Name",
            "stock":"in stock"
         }
}

Upvotes: 0

Views: 193

Answers (1)

iliyan
iliyan

Reputation: 76

according to php.net if you use something as:

$decodedArray = json_decode($jsonString,true);

And according to your example to access the stock value you will need:

foreach($decodedArray['items'] as $itemKey=>$itemProps)
{
  echo $itemProps['stock'];
}

And as I tested it, your json is invalid. You must add one more curly bracket add the end of your json to close the main root object :)

Upvotes: 1

Related Questions