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

WBOY
Release: 2016-06-13 13:26:15
Original
1114 people have browsed it

数组搜索的时候怎么样才能不区分大小写呢?
为什么下面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";
}
?>
Copy after login


------解决方案--------------------
如果 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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!