0

I have the following JSON object:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
   0: Object
      category: Object
         0: "Value"

I was wondering how I could get 'Value' in a variable in PHP. I thought category[0] would just do it but I get the following instead of 'Value':

 __proto__: Object 

What should I do?

8
  • Do you mean you have a javascript object (or array of objects it might appear) you want to pass to PHP? What you show is not JSON (JSON is nothing more than a serialization format). What is the actual mechanism proposed for passing the data (i.e. AJAX call)? Commented Nov 11, 2013 at 20:18
  • [{"category":{"0":"Value"} < You mean this? Commented Nov 11, 2013 at 20:26
  • If that's how your JSON looks like it would be $var = json_decode($json); $var[0]['category']['0'] Commented Nov 11, 2013 at 20:27
  • Mhm, I get 'null' instead of the value .. Commented Nov 11, 2013 at 20:33
  • oh wait, try without the '' on the second 0 - $var[0]['category'][0]. Can you paste a larger snipped of your php code? Commented Nov 11, 2013 at 20:34

1 Answer 1

1

You have to converts the JSON string into associative array, which is done by json_decode() with second parameter set as true.

$var = json_decode($json, true); 
$value = $var['0']['category']['0'];
Sign up to request clarification or add additional context in comments.

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.