如何在PHP中的数组中找到一个字符串?
发布时间:2020-12-13 14:06:17 所属栏目:PHP教程 来源:网络整理
导读:假设我在 PHP中有一个数组: $array = array("apple","banana","cap","dog",etc..) up to 80 values. 和一个字符串变量: $str = "abc"; 如果我想检查这个字符串($str)是否存在于数组中,我使用preg_match函数.这是这样的: $isExists = preg_match("/$str/",
假设我在
PHP中有一个数组:
$array = array("apple","banana","cap","dog",etc..) up to 80 values. 和一个字符串变量: $str = "abc"; 如果我想检查这个字符串($str)是否存在于数组中,我使用preg_match函数.这是这样的: $isExists = preg_match("/$str/",$array); if ($isExists){ echo "Its exists"; } else { echo "Not exixts" } ; 是正确的方法吗?如果阵列越来越大,会很慢吗?还有其他方法吗?我试图缩小我的数据库流量.希望你们能帮忙.. 得到答案后: bool in_array ( mixed $needle,array $haystack [,bool $strict ] ) http://php.net/manual/en/function.in-array.php (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |