php中的json_decode,索引0被忽略

时间:2015-01-28 10:47:14

标签: php json

我遇到了一个非常奇怪的问题,我在2天内无法解决。

while ($rs_sub = $resultSub->fetchRow()) {
$i=0;
$new = json_decode($rs_sub['content'],1);
foreach ($new as $value) {
    if (!(empty($value['type'])) && !($value['type'] == 'captcha') && !($value['label'] == 'files') && !($value['type'] == 'hidden') && !($value['label'] == 'divider')) {
        if (($value['type'] == 'radio' || $value['type'] == 'check' || $value['type'] == 'matrix' || $value['type'] == 'stars' || $value['type'] == 'smiley' || $value['type'] == "thumbs") && $value['value']!="") {
            if (!in_array($value['label'], $label)) {
                $label[] = $value['label'];
            }
            if ($value['type'] == "thumbs") {
                if($value['value']=="1"){
                    $value['value']="Like";
                }else{
                    $value['value']="Dislike";
                }
            }
            if (!in_array($value['value'], $choice[$value['label']])) {
                    $choice[$value['label']][] = $value['value'];
            }
            $var=array_search($value[value], $choice[$value['label']]);
            if (array_search($value['value'], $choice[$value['label']])) {
                echo $var;
                $ans[$value['label']][$var] = $ans[$value['label']][$var] + 1;
            } else {
                $ans[$value['label']][$var] = 1;
            }
        }
    }
}}

这是输出。

11111111111122331331331331331331331331331343413413413413413413413411211211212412431131421311

应该是

00011111111111102020033133133133133133133133133134034134134134134134134134112112112124000012431013014201030100001

在if条件中忽略了所有0,我不知道为什么会发生这种情况。 我已经尝试过(字符串),strval()和其他许多但没什么可行的。

注意:$ rs_sub [' content']是我的数据库中的一个字段。

修改: 如果我把" echo $ var"在if else条件之外,它将起作用(0正在回显)。

编辑2 这是我在db

中的json代码
{"1":{"label":"pkid","value":"undefined","type":null,"validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null},"3":{"label":"","value":"","type":null,"validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null},"4":{"label":"Service","value":"4","type":"stars","validation":"","required":"0","min":"","max":"","tooltip":"field0","custom":null,"custom2":null,"custom3":"zz","custom4":null},"5":{"label":"Service-Reason","value":"","type":"stars","validation":"","required":"0","min":"","max":"","tooltip":"field0","custom":"reason","custom2":null,"custom3":"zz","custom4":null},"6":{"label":"Food","value":"5","type":"smiley","validation":"","required":"0","min":"","max":"","tooltip":"field1","custom":null,"custom2":null,"custom3":"zz","custom4":null},"7":{"label":"Food-Reason","value":"","type":"smiley","validation":"","required":"0","min":"","max":"","tooltip":"field1","custom":"reason","custom2":null,"custom3":"zz","custom4":null},"8":{"label":"Like it?","value":"1","type":"thumbs","validation":"","required":"0","min":"","max":"","tooltip":"field2","custom":null,"custom2":null,"custom3":"zz","custom4":null},"9":{"label":"pkid","value":"1913","type":null,"validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null},"10":{"label":"title","value":"Form Title","type":null,"validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null},"0":{"custom3":"zz","label":"","value":null},"2":{"custom3":"zz","label":"","value":null}}

1 个答案:

答案 0 :(得分:0)

答案显而易见:if条件为if ($var)。当$var == 0条件评估为FALSE并且它运行else的{​​{1}}分支时。但是在else分支上没有if