1

Well this was a 7 years old question here and I need the answer from this thread no satisfying answer was given: getting value from json string php someone should kindly assist as my code is of this format:

after json_decode()

var_dump gave this as output:

 String(384) “{“data:{“foo”: bar, “foo1”: “bar1”, “foo2”: bar2, }}

Note that bar is int, bar1 string etc.

How can I get "foo1": "bar1" from this string; generally how can I get values from setups like this in PHP?

3
  • 1
    Are you sure you called var_dump(json_decode($value))? Commented Jun 4, 2020 at 22:04
  • It won't work with th crazy wacked out quotes ““ And the fact that the first { is quoted. This is not valid JSON. Commented Jun 4, 2020 at 22:11
  • Exactly I checked it with <jsonlint.com > and it says invalid json and I copied the whole data and checked it on it..the same format. Commented Jun 4, 2020 at 22:20

1 Answer 1

1

Once you get your JSON object, you can access foo1 simply like this:

$str = '{"data": {"foo": bar, "foo1": "bar1", "foo2": bar2}}';
$json = json_decode($str);
echo $json->data->foo1;
Sign up to request clarification or add additional context in comments.

3 Comments

Okay Let me try this out.
wow this worked: I was doing something like this $var =var_dump(json_decode($data true)); and it was giving string. I had to remove it from the variable and remove true. But I am not sure why it doesn't work that way. I appreciate your input.
I tried yesterday but they said I am just new to the site. It is recorded but the public can't see it :(

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.