Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm writing a PHP script to work with some JSON data. Below is an (abridged) var_dump($data). I want to return the value associated with ["[question(13), option(0)]"] which is 20. I can't figure out how to do it. I've tried $data->[question(13), option(0)] and $data->question(13). (I tried to look this up but I'm not sure what the notation means, so I'm not sure what I'm looking for)

object(stdClass)#133 (36) {
  ["id"]=>
  string(1) "1"
  ["contact_id"]=>
  string(0) ""
  ["status"]=>
  string(8) "Complete"
  ["is_test_data"]=>
  string(1) "0"
  ["datesubmitted"]=>
  string(19) "2012-04-19 17:11:00"
  ["[question(5)]"]=>
  string(11) "C.    40%, 40%"
  ["[question(9)]"]=>
  string(47) "D.    EBITDA and Free cash flow are the same thing"
  ["[question(10)]"]=>
  string(48) "A.    Accounts Payable as % of sales would increase"
  ["[question(11)]"]=>
  string(20) "E.    None of the above"
  ["[question(12)]"]=>
  string(97) "A.     A larger portion of initial investment is equity which can increase exit return potential."
  ["[question(13), option(0)]"]=>
  string(2) "20"
  ["[url("embed")]"]=>
  string(0) ""
  ["[variable("STANDARD_IP")]"]=>
  string(13) "38.107.74.230"
  ["[variable("STANDARD_LONG")]"]=>
  string(10) "-73.976303"
  ["[variable("STANDARD_LAT")]"]=>
  string(9) "40.761902"
}
share|improve this question
You should really have your properties follow common variable name conventions. – Jason McCreary Apr 25 '12 at 17:32

2 Answers

up vote 3 down vote accepted

Either use extended object access notation:

$data->{'[question(13), option(0)]'}

Or just ask for normal array and use it as normal array.

json_decode($json, true);
share|improve this answer
Very good answer. Couldn't say it better. – powtac Apr 25 '12 at 17:31
Ahhh... the infamous curly brackets. Thanks! Out of curiosity, what does that notation mean? Are those instance variables of an object? – Emerson Apr 25 '12 at 17:58
@Emerson: Yes, they are. Check the stackoverflow.com/questions/3737139/… for more information. – GlitchMr Apr 25 '12 at 18:00

try this

echo $data->{'[question(13), option(0)]'};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.