如何找到在PHP中的数组一个字符串? [英] How to find a string in an array in PHP?

查看:245
本文介绍了如何找到在PHP中的数组一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我在PHP中的数组:

  $阵列=阵列(苹果,香蕉,上限,狗,等..)80值。

和一个字符串变量:

  $海峡=ABC;

如果我想查这个字符串( $ STR )无论是在数组中的存在与否,我用preg_match功能。这是这样的:

  $ isExists = preg_match(/ $ STR /,$阵列);如果($ isExists){    回声它的存在;
}其他{ 回声不exixts};

这是不是正确的方法是什么?如果数组变大,这将是很慢?还有什么其他的方法?我试图缩小我的数据库流量。希望你们能帮助..

后得到答案:
如果我有两个或两个以上的字符串比较?那怎么办?


解决方案

布尔in_array(混合$针,数组$草垛[ ,布尔$严格])

<一个href=\"http://php.net/manual/en/function.in-array.php\">http://php.net/manual/en/function.in-array.php

Lets assume I have an array in PHP:

$array = array("apple", "banana", "cap", "dog", etc..) up to 80 values.

and a string variable:

$str = "abc";

If I want to check whether this string ($str) is exists in the array or not, I use preg_match function. Which is like this:

$isExists = preg_match("/$str/", $array);

if ($isExists){

    echo "Its exists";
} else {

 echo "Not exixts" 

} ;

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. Hope you guys can help..

AFTER GET THE ANSWER: If i have two or more string to compare? How to do that?

解决方案

 bool in_array  ( mixed $needle  , array $haystack  [, bool $strict  ] )

http://php.net/manual/en/function.in-array.php

这篇关于如何找到在PHP中的数组一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆