0
$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},\
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},\
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},\
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},\
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

echo $response['items'][1]['food'];

Error: "Illegal string offset 'items'

Why can't I access the data?

4
  • 1
    Did you decode the string with json_decode first? Your question does not show it Commented Jul 26, 2018 at 5:10
  • I printed the string before and after decoding it, no difference. Commented Jul 26, 2018 at 5:13
  • 1
    can you look at my answer? Commented Jul 26, 2018 at 5:20
  • 1
    Spot on Andreas, thank you! Commented Jul 26, 2018 at 5:26

5 Answers 5

4

You must decode a json string before using it as an object/array.
Your question shows you want to use it as an array, that means the second parameter of json_decode should be true.
However the \ makes your string invalid. Is that copy paste error or is that the actual string?
If it is the actual string then you may need to remove the \ before decoding.
I assume it's a copy paste error?

$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

$arr = json_decode($response, true);
echo $arr['items'][1]['food']; // beans

https://3v4l.org/RiqSo


If you need to remove the unwanted \ you can use stripslashes.

$arr = json_decode(stripslashes($response), true);

https://3v4l.org/WslYh


To replicate the error you get you need to do the following:

$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},\
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},\
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},\
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},\
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

$arr = (string)json_decode($response, true); // json_decode returns NULL and the string cast makes it "NULL"
echo $arr['items'][1]['food']; // Warning: Illegal string offset 'items'
// or
echo $response['items'][1]['food']; // Warning: Illegal string offset 'items'
Sign up to request clarification or add additional context in comments.

11 Comments

The true tells json_decode how to format the output, either as an array (true) or as an object (false). The error is because json_decode can't decode an array that is invalid and returns NULL. So when $response is NULL there is no [items] and that is why the parser says "Illegal string offset 'items'
Yes. If you want to use it as an object then you need to use -> between the items
Any reason why you need curl? I usually use file_get_contents if I can. Only when I need to spoof agent I use Curl.
I would say so. But essentially both should return the string. $response = file_get_contents($URL); What does the $response contain? Make sure to var_dump to make sure there is no spaces or something that creates the error
With or without `\`? Try the stripslashes code and see if that works.
|
2

Try this

$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

$response_json = json_decode($response);
echo $response_json->items[2]->food.' | '.$response_json->items[2]->condition;

output : yam | not paid

1 Comment

Yeah that works well. I prefer to use arrays so I just needed to add the 'true' parameter to my json_decode.
1

decode your string into json using json_decode.

http://php.net/manual/en/function.json-decode.php refer this link for more infomation

4 Comments

Sorry but I get the error whether I decode it or not.
Can't you keep an array with keys and decode it to json. That works pretty cool.
json_decode($response, true); - I was missing the true part :(
Happy to hear that you've figured it out :-)
1

You need to convert first json into array to using json_decode() function.

$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

$getarray = json_decode($response);

echo $getarray->items[0]->food;

Comments

1

The data which you accessing it is in json format. So first you have to convert it into normal Array using json_decode();

Here is the code how.

$response = json_decode($response, true);

Now it is array. So can access its data like this way.

echo $response['items'][1]['food']; 

Output :

beans

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.