为什么PHP函数“implode”返回字符串“array”?

时间:2016-06-05 15:28:22

标签: php arrays

我正在调用以下函数:

function buildInStmt($array)
{
    if (is_array($array)) {
        $in = implode(',', $array);
    } else $in = "'" . $array . "'";
    var_dump("in=" . $in); 
    var_dump("$array=" . $array); 
    return $in;
}

...

    if (!isset($_REQUEST["form-type"]) || empty($_REQUEST["form-type"])) {
        throw new Exception('You must select a form type.');       
    }
    $forms = buildInStmt(array($_REQUEST["form-type"]));

函数的var_dumps返回:

string'in = Array'(length = 8)

string'Array = Array'(length = 11)

string'in = Array'(length = 8)

string'Array = Array'(length = 11)

string'in = 1'(length = 4)

string'Array = Array'(length = 11)

var_dump(array($ _ REQUEST [“form-type”]));来自通话回报:

数组(大小= 1)   0 =>     数组(大小= 2)       0 =>字符串'4'(长度= 1)       1 =>字符串'7'(长度= 1)

为什么不内爆返回逗号分隔的字符串?

如果你选择对这个问题进行选择,请解释原因,以便我可以从错误中吸取教训。

1 个答案:

答案 0 :(得分:0)

array (size=1) 0 => array (size=2) 0 => string '4' (length=1) 1 => string '7' (length=1)

大致相当于

$var = [
  0 => [
    0 => '4',
    1 => '8'
  ]
]

所以看起来你有一个数组数组。请不要在代码中添加不需要的数组:

buildInStmt($_REQUEST["form-type"])