0

I am having a hard time getting the value of an array in JSON to php.

Here is the JSON format I'm trying to retrieve:

{
 "products": [
  {
   "product_id": 18647,
   "model_code": "CVADA-M702-Black-2GEN"
  }
  {
   "product_id": 14343,
   "model_code": "CDEP-Q123-Blue-3GEN"
  }
 ]
}

and I'm trying to retrieve it like this but to no avail:

$response = json_decode($json_response, true);
        foreach($response as $res)
        {
            print $res->products->product_id;
        }

It works though if I'm only trying to input a singl output:

$response = json_decode($json_response, true);
print $response['products'][0]['product_id'];

So, can anyone help me how to properly retrieve JSON value to php?

6
  • The $response variable will be an associative array not an object. Commented Aug 12, 2015 at 10:11
  • use print $response->products[0]->product_id Commented Aug 12, 2015 at 10:12
  • array.include-once.org/… Commented Aug 12, 2015 at 10:13
  • the $response variable will not an object . it is array. or json_decode($json_response, false); use this syntax. Commented Aug 12, 2015 at 10:15
  • Apart from what is all ready said, In your two examples you are using two completely different methods to access the data, If one of them doesn't work and the other does, why not use the one at works? Commented Aug 12, 2015 at 10:33

3 Answers 3

2

The JSON data you have missed the , between the two array. It should be

{
 "products": [
  {
   "product_id": 18647,
   "model_code": "CVADA-M702-Black-2GEN"
  },
  {
   "product_id": 14343,
   "model_code": "CDEP-Q123-Blue-3GEN"
  }
 ]
}

and another problem is you are accessing the array as an object , as you are decoding the JSON data as below it returns an array :

$response = json_decode($json_response, true);

it will return array.

$response = json_decode($ds, true);

try accessing value as below:

foreach($response['products'] as $res)
{
    print $res['product_id'];
}

for more detail check out the first example provided here :

http://php.net/manual/en/function.json-decode.php

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thank you for the answer. Thanks for the knowledge!
2

Your JSON format code has an small error, there should be ',' after following section.

 {    
    "product_id": 18647,   
    "model_code": "CVADA-M702-Black-2GEN"  
 },

Please check following code:

{
 "products": [
  {
   "product_id": 18647,
   "model_code": "CVADA-M702-Black-2GEN"
  },
  {
   "product_id": 14343,
   "model_code": "CDEP-Q123-Blue-3GEN"
  }
 ]
}

You can also check your JSON code on http://json.parser.online.fr/

Comments

1

Try this,

$response = json_decode('{
 "products": [
  {
   "product_id": 18647,
   "model_code": "CVADA-M702-Black-2GEN"
  },
  {
   "product_id": 14343,
   "model_code": "CDEP-Q123-Blue-3GEN"
  }
 ]
}');

foreach ($response->products as $res) {
    echo $res->product_id;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.