json-encode错误仅在某些mysql记录查找中

时间:2016-02-11 03:16:27

标签: php json

我收到此错误

  

PHP警告:为foreach()提供的参数无效

这一行

{$ret = json_encode_jsfunc($value, $funcs, 1);$input[$key]=$ret[0];$funcs=$ret[1];}

这是

的一部分
/**
 * Function to encode JS function reference from PHP array
 * http://www.php.net/manual/en/function.json-encode.php#105749
 */
function json_encode_jsfunc($input=array(), $funcs=array(), $level=0)
{
    foreach($input as $key=>$value)
    {
        if (is_array($value))
        {
            $ret = json_encode_jsfunc($value, $funcs, 1);
            $input[$key]=$ret[0];
            $funcs=$ret[1];
        }
        else
        {
            if (substr($value,0,8)=='function')
            {
                $func_key="#".rand()."#";
                $funcs[$func_key]=$value;
                $input[$key]=$func_key;
            }
            // for json data, incase of local array
            else if (substr($value,0,2)=='[{')
            {
                $func_key="#".rand()."#";
                $funcs[$func_key]=$value;
                $input[$key]=$func_key;
            }
        }
    }
    if ($level==1)
    {
        return array($input, $funcs);
    }
    else
    {
        $input_json = json_encode($input);
        foreach($funcs as $key=>$value)
        {
            $input_json = str_replace('"'.$key.'"', $value, $input_json);
        }
        return $input_json;
    }
}

/*

此错误是暂时的。

仅在某些情况下发生。

我查看了传入此功能的数据库记录,导致此问题的记录与不存在此类问题的记录之间没有区别。但是,它始终是导致错误的相同记录。

正在调用代码:

// multiple search group function
        if ($this->actions["search"] == "group")
        {
            $this->options["search_options"]["multipleSearch"] = true;
            $this->options["search_options"]["multipleGroup"] = true;
        }

        $this->options["search_options"]["sopt"] = array('eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc','nu','nn');

        $out = json_encode_jsfunc($this->options);
        $out = substr($out,0,strlen($out)-1);

<?php ### P ### ?>
        if (typeof(opts) != 'undefined') extra_opts = opts;
        if (typeof(opts_<?php echo $grid_id?>) != 'undefined') extra_opts = opts_<?php echo $grid_id?>;

        // if bootstrap, increase subgrid icon width
        if (jQuery("link[href*='ui.bootstrap']").length)
            extra_opts["subGridWidth"] = "33px";

        var grid_<?php echo $grid_id?> = jQuery("#<?php echo $grid_id?>").jqGrid( jQuery.extend(<?php echo $out?>, extra_opts ) );

        jQuery("#<?php echo $grid_id?>").jqGrid('navGrid','#<?php echo $grid_id."_pager"?>',
                <?php echo json_encode_jsfunc($this->navgrid["param"])?>,
                <?php echo json_encode_jsfunc($this->options["edit_options"])?>,
                <?php echo json_encode_jsfunc($this->options["add_options"])?>,
                <?php echo json_encode_jsfunc($this->options["delete_options"])?>,
                <?php echo json_encode_jsfunc($this->options["search_options"])?>,
                <?php echo json_encode_jsfunc($this->options["view_options"])?>
                );

// Set grouping header using callGridMethod
        <?php if (!empty($this->group_header)) { ?>
        jQuery("#<?php echo $grid_id?>").jqGrid("setGroupHeaders", <?php echo json_encode_jsfunc($this->group_header)?>);
        <?php } ?>

以及

jQuery('#<?php echo $grid_id?>').jqGrid('editGridRow', ids, <?php echo json_encode_jsfunc($this->options["edit_options"])?>);

带来错误的记录示例:

{
        "idstaging" : 17,
        "category" : "nutrigenetics",
        "subcategory" : "methylation",
        "gene" : "MTHFR",
        "rs" : "rs1801133",
        "taqman" : "C___1202883_20",
        "var_alias" : "C677T",
        "notes" : ""
    }

不会带来错误的记录示例

{
    "idstaging" : 20,
    "category" : "nutrigenetics",
    "subcategory" : "methylation",
    "gene" : "MTRR",
    "rs" : "rs1801394",
    "taqman" : "C___3068176_10",
    "var_alias" : "A66G",
    "notes" : ""
}

0 个答案:

没有答案