Heim  >  Artikel  >  Backend-Entwicklung  >  数组搜索的时候如何样才能不区分大小写呢

数组搜索的时候如何样才能不区分大小写呢

WBOY
WBOYOriginal
2016-06-13 10:29:361240Durchsuche

数组搜索的时候怎么样才能不区分大小写呢?
为什么下面2个都搜不到啊,我看了手册说加个false可以的,为什么还不行?

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $dr=array('cc','Dd','Ee');if(in_array("dd",$dr,false)){    echo "aa";}if(array_search("dd",$dr,false)){    echo "aa";}?>


------解决方案--------------------
如果 needle 是字符串,则比较是区分大小写的。
如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。 


------解决方案--------------------
in_array

可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。

值的类型是否相同 不是区分大小写

array_search

如果第三个参数 strict 被指定为 true,则只有在数据类型和值都一致时才返回相应元素的键名。

也是类型和值,并没有说到区分大小写的事情.

按照你的需求

1.遍历 使用字符串比较函数
2.正则
3.全部转换成小写后比较
....

------解决方案--------------------
第三个参数是判断类型的,也就是用===,而不是==。。

封个函数遍历一下行了.
------解决方案--------------------
麻烦你自己看php.net,这种基础问题你将来遇见成百上千个,你都来问吗
------解决方案--------------------
探讨

引用:

如果 needle 是字符串,则比较是区分大小写的。
如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。

我想问下in_array函数在什么情况下不区分大小写?
我换成数组还是区分大小写。
$dr=array('cc','Dd','Ee');
$vr=arr……

------解决方案--------------------
自己写一个就是了
PHP code
$dr = array('cc','Dd','Ee');if(in_iarray("dd",$dr,false)){    echo "aa";}function in_iarray($needle, $haystack, $strict=false) {  if(! is_string($needle)) return in_array($needle, $haystack, $strict);  return in_array(strtolower($needle), array_map('strtolower', $haystack));}<div class="clear">
                 
              
              
        
            </div>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn