1

I have this data

$result = json_decode(curl_exec($ch));
print_r($result);
// stdClass Object ( [d] => {"id":10} )
$id = ?;

How can I get a value of id?

2 Answers 2

2

Since $result is an object, you have to use property notation to access its components.

$id = json_decode($result->d)->id;

You need the extra json_decode because the value of $result->d is another JSON string, not an object or array.

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

4 Comments

It doesn't write me the id, it writes just {
See my updated answer. It would be better if you used var_dump instead of print_r, so the types of all the values would be more obvious.
You may want to check the application that created the original JSON, as it's strange to encode the values before encoding the whole structure. That's why the double decode is necessary.
yes, that "d" value is string need to convert in to json object
0

You would get value with following

$obj_result = json_decode($result->d);
echo $obj_result->id;

By this way you will get id value 10

3 Comments

It doesn't write me the id, it writes just {
Put echo before the statement, that will show result of id. echo will show 10 on your Browser.
The value of $result->d is the string '{"id":10}', not an array, so you can't index it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.