如何在 PHP 中的数组中查找字符串? [英] How to find a string in an array in PHP?
本文介绍了如何在 PHP 中的数组中查找字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组:
$array = array("apple", "banana", "cap", "dog", etc..) up to 80 values.
和一个字符串变量:
$str = "abc";
如果我想检查这个字符串($str
)是否存在于数组中,我使用preg_match
函数,它是这样的:
If I want to check whether this string ($str
) exists in the array or not, I use the preg_match
function, which is like this:
$isExists = preg_match("/$str/", $array);
if ($isExists) {
echo "It exists";
} else {
echo "It does not exist";
}
这是正确的方法吗?如果数组变大,会不会很慢?有没有其他方法?我正在尝试缩减我的数据库流量.
Is it the correct way? If the array grows bigger, will it be very slow? Is there any other method? I am trying to scaling down my database traffic.
如果我有两个或多个字符串要比较,我该怎么做?
And if I have two or more strings to compare, how can I do that?
推荐答案
bool in_array ( mixed $needle , array $haystack [, bool $strict ] )
http://php.net/manual/en/function.in-数组.php
这篇关于如何在 PHP 中的数组中查找字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文