I am using implode()
like this
我就是这样使用Intode()的
$var = implode(',', $dataColumns);
and get a strange error
并得到一个奇怪的错误
Uncaught TypeError: implode(): Argument #1 ($pieces) must be of type array, string given
更多回答
Hi there! Welcome to Stackoverflow! I noticed that your question might need a bit more context to make it clearer. Can you please provide some additional details so that the community can better understand what you're asking? Thanks!
你好啊!欢迎来到Stackoverflow!我注意到你的问题可能需要更多的上下文来使它更清楚。你能提供一些额外的细节,以便社区可以更好地理解你的要求吗?谢谢!
This implode
looks correct ! maybe the issue is in other part of the code
这次爆炸看起来是正确的!也许问题出在代码的其他部分
Did you check whether any other parts of your code throw that error? I can't reproduce it
你检查过你的代码的其他部分是否抛出了这个错误吗?我不能复制它
This error message is not very clear and rather misleading. As far as I know, the message will be fixed in the future, to be made more clear.
此错误消息不是很清楚,并且相当具有误导性。据我所知,这一信息将在未来得到修复,以得到更明确的说明。
It actually means that you are passing null
instead of array to implode.
这实际上意味着您正在传递NULL而不是要内爆的数组。
And the error makes sense, because a null is not an array, and one shouldn't implode a null.
这个错误是有意义的,因为空值不是数组,不应该内爆空值。
So the error is coming from the fact that implode has two signatures, one involves two parameters and one just a single parameter:
因此,错误来自于内爆有两个签名,一个涉及两个参数,另一个只涉及一个参数:
// just two familiar parameters, a glue and array
implode(string $separator, array $array): string
// Alternative signature, array only without a glue
implode(array $array): string
And when implode is called with two parameters, but the the second one is null
, implode thinks it is called with just one (which has to be an array), and thus complains, emitting the error you see.
当使用两个参数来调用implode,但第二个参数为NULL时,implode会认为只有一个参数(必须是一个数组)来调用它,因此会发出抱怨,发出您所看到的错误。
To fix this error, simply make sure that implode isn't called on a null value, as it makes no sense whatsoever.
要修复此错误,只需确保不会对空值调用内部函数,因为它没有任何意义。
As a last resort, if you have no control on the code that provides the array, you can silence this error with a workaround like this
作为最后的手段,如果您无法控制提供数组的代码,您可以使用如下解决方法来消除此错误
implode($separator, $array ?? []);
When $array
is null
, this code will return just an empty string. But you must understand that it's just as bad as any other error suppression. So again, it's better to fix the error, not the symptom.
当$ARRAY为空时,此代码将仅返回空字符串。但您必须明白,它和任何其他错误抑制一样糟糕。因此,更好的做法是修复错误,而不是修复症状。
更多回答
我是一名优秀的程序员,十分优秀!