PHP 警告:in_array() 中第二个参数的数据类型错误
我最近在我的错误日志中收到此错误:
PHP Warning: in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument in... on line 423
并引用了以下代码:
<?php foreach($services_a as $key=>$service) { ?>
<div class="oneservice">
<input type="checkbox" name="services[]" value="<?php echo $key; ?>" id="service<?php echo $key; ?>"<?php if( in_array($key, $services) ) { echo ' checked="checked"'; } ?> />
<label for="service<?php echo $key; ?>"><?php echo $service; ?></label>
</div>
非常欢迎任何意见, 谢谢
I recently got this error on my error log:
PHP Warning: in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument in... on line 423
and refers to the following piece of code:
<?php foreach($services_a as $key=>$service) { ?>
<div class="oneservice">
<input type="checkbox" name="services[]" value="<?php echo $key; ?>" id="service<?php echo $key; ?>"<?php if( in_array($key, $services) ) { echo ' checked="checked"'; } ?> />
<label for="service<?php echo $key; ?>"><?php echo $service; ?></label>
</div>
Any views are very welcome,
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
in_array()
检查值,而不是键。如果 xou 想要检查密钥,请使用
array_key_exists()
:当您第一次打开表单时,您的
$_POST['services']
将为空。要克服该错误,如果 post 没有任何内容,请初始化一个空数组:in_array()
checks for the value, not the key.Use
array_key_exists()
if xou want to check for the key:When you open the form the first time, your
$_POST['services']
will be empty. to overcome the error, initialize an empty array if nothing is coming from post:你应该检查“一个数组”
它是一个数组吗?
You should check "an array"
Is it an array?
可能是因为您调用了正在迭代
$services_a
的数组,但在in_array()
中用作第二个参数的数组被称为普通的$services
。问题可能是第二个参数有一个 NULL 值,这会向您发出警告。Probably because you called the array you are iterating for
$services_a
but the array you use for second argument inin_array()
is called plain$services
. Problem is probably that the second argument has a NULL value, which gives you the warning.