更改被称为PHP的对象字符串

时间:2015-07-01 08:25:25

标签: php

我有一个这种形式的对象:

{"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"}

我想循环遍历每个q(所以q1,q2,q3等);

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

这不起作用,因为它必须是

$tobeparsed->q1;

但是它是

$tobeparsed->"q1";

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

json_decode返回一个StdObject。

如果你想获得一个数组而不是使用它:

json_decode($tobeparsed, true);