0

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"
         }
}
1
  • 3
    It should be $json['items']['item000']['stock'] after it's been parsed (assuming $json = json_decode('...');). Jan 20, 2011 at 21:16

1 Answer 1

1

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 :)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.