教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php in_array()函数使用注意事项

php in_array()函数使用注意事项

发布时间:2016-10-14   编辑:jiaochengji.com
教程集为您提供php in,array()函数使用注意事项等资源,欢迎您收藏本站,我们将为您提供最新的php in,array()函数使用注意事项资源
in_array函数是用来判断我们输入的值是否在指定数组值中存在了,但我们在使用时如果指定值为0或空时我们会发现判断并不确定了,下面我来给大家介绍此问题的原因与例子。

0.in_array常用的使用方法

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4477')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4477>

<?php
$nameslist = array("小皮皮", "小轩轩", "工了以", "可爱的小贸");

if (in_array("工了以",$nameslist))
  {
  echo "找到呀";
  }
else
  {
  echo "不知道在哪里找,找不到";
  }
?>

输出的值为

找到呀


1.期望输出的是false,但实际上是true

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5260')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5260>

<?php
$value='';
$falsehortrue=in_array($value,array(0,1));
var_dump($falsehortrue);
//bool(true)
?>

2.使用in_array的第三个类型比较参数后,输出结果与期望相同输出的是false

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9231')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9231>

<?php
$value='';
$falsehortrue=in_array($value,array(0,1),true);
var_dump($falsehortrue);
// bool(false)
?>

总结,php in_array函数对于空值与0或1时我们需要带参数 true来验证哦,否则可能出现准情况。

您可能感兴趣的文章:
php数组函数in_array()检查数组中是否存在某值
php中IN_ARRAY函数的注意事项
php数组函数 in_array 的用法及注意事项
php in_array()函数使用注意事项
php in_array,trim,isset,unset,is_numeric函数用法。
php如何判断一个值是否在数组中
php数组函数in_array() 查找数组值是否存在
PHP array_search 和 in_array 函数效率问题
php数组查找函数in_array()、array_search()、array_key_exists()
php数组入门教程之in_array()函数

[关闭]
~ ~