0

I have an object in this form:

{"q1":0,"q2":1,"q3":0,"q4":1,"q5":2,"q6":1,"q7":1,"q8":1,"q9":1,"q10":2,"q11":1,"q12":0,"q13":0,"q14":1,"q15":1,"q16":1,"q17":0,"q18":0,"q19":1,"q20":1,"q21":1,"q22":0,"q23":0,"qc1":[3,5,6,7],"qc2":[6],"qi1":"Good","qi2":"Bad","qi3":"Funny","qi4":"Hello"}

and I want to loop through each of the q's (so q1, q2, q3 etc.);

$tobeparsed = json_decode($result['surveyJSON']);
for($i=1; $i<23;$i++){
    $temp = "q".$i;
    $q[$i]=$tobeparsed->$temp;
}

This doesn't work because it has to be

$tobeparsed->q1;

but instead it is

$tobeparsed->"q1";

How do I fix this?

2
  • Use eval() php.net/manual/en/function.eval.php Commented Jul 1, 2015 at 8:26
  • This is not what you need. Avoid eval like fire. Take a look at Andrew Answer Instead Commented Jul 1, 2015 at 8:31

1 Answer 1

4

json_decode returns an StdObject.

If you want to get an array instead use this:

json_decode($tobeparsed, true);

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.