JBaoz
JBaoz

Reputation: 298

Change string of object being called PHP

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?

Upvotes: 0

Views: 73

Answers (1)

Andrei
Andrei

Reputation: 3588

json_decode returns an StdObject.

If you want to get an array instead use this:

json_decode($tobeparsed, true);

Upvotes: 4

Related Questions