Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组\u键或不区分数组中的\u键无法正常工作_Php_Arrays_Case Insensitive_Array Key - Fatal编程技术网

Php 数组\u键或不区分数组中的\u键无法正常工作

Php 数组\u键或不区分数组中的\u键无法正常工作,php,arrays,case-insensitive,array-key,Php,Arrays,Case Insensitive,Array Key,我有一个不敏感的数组\u键和数组中的问题。。。 我正在开发一个翻译器,我有这样的东西: $wordsExample = array("example1","example2","example3","August","example4"); $translateExample = array("ejemplo1","ejemplo2","ejemplo3","Agosto","ejemplo4"); function foo($string,$strict=FALSE) { $key

我有一个不敏感的数组\u键和数组中的问题。。。 我正在开发一个翻译器,我有这样的东西:

$wordsExample = array("example1","example2","example3","August","example4");
$translateExample = array("ejemplo1","ejemplo2","ejemplo3","Agosto","ejemplo4");


function foo($string,$strict=FALSE)
{
    $key = array_keys($wordsExample,$string,$strict);
    if(!empty($key))
      return $translateExample[$key[0]];
    return false;
}

echo foo('example1'); // works, prints "ejemplo1"
echo foo('august');  // doesnt works, prints FALSE
我使用in_阵列进行了测试,结果相同…:

function foo($string,$strict=FALSE)
{
    if(in_array($string,$wordsExample,$strict))
      return "WOHOOOOO";
    return false;
}

echo foo('example1'); //works , prints "WOHOOOOO"
echo foo('august'); //doesnt works, prints FALSE

创建数组并使用以下内容查找键:

另一种方法是在_array函数中编写一个不区分大小写的新

function in_arrayi($needle, $haystack) {
    return in_array(strtolower($needle), array_map('strtolower', $haystack));
}

如果您希望它使用更少的内存,最好使用小写字母创建单词数组。

我创建了一个小函数,用于测试干净的URL,因为它们可以是大写、小写或混合的:

function in_arrayi($needle, array $haystack) {

    return in_array(strtolower($needle), array_map('strtolower', $haystack));

}

这种方法很简单。

是的,这是一种方法,但如果要做很多翻译,系统可能会占用很多内存。您不需要创建两个数组,只创建一个数组,而是使用小写字母创建它。这样,您就不必使用array_map()创建另一个instanceconsider来将数组项小写
function in_arrayi($needle, array $haystack) {

    return in_array(strtolower($needle), array_map('strtolower', $haystack));

}