如何在 PHP 中搜索多个值的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1212605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search Array for multiple values in PHP?
提问by Jens T?rnell
I need to get the keys from values that are duplicates. I tried to use array_search and that worked fine, BUT I only got the first value as a hit.
我需要从重复的值中获取键。我尝试使用 array_search 并且效果很好,但我只得到了第一个值作为命中。
I need to get both keys from the duplicate values, in this case 0 and 2. The search result output as an array would be good.
我需要从重复值中获取两个键,在本例中为 0 和 2。作为数组输出的搜索结果会很好。
Is there a PHP function to do this or do I need to write some multiple loops to do it?
是否有 PHP 函数可以执行此操作,或者我是否需要编写多个循环来执行此操作?
$list[0][0] = "2009-09-09";
$list[0][1] = "2009-05-05";
$list[0][2] = "2009-09-09";
$list[1][0] = "first-paid";
$list[1][1] = "1";
$list[1][2] = "last-unpaid";
echo array_search("2009-09-09",$list[0]);
回答by Adam Wright
You want array_keyswith the search value
你想要带有搜索值的array_keys
array_keys($list[0], "2009-09-09");
which will return an array of the keys with the specified value, in your case [0, 2]. If you want to find the duplicates as well, you can first make a pass with array_unique, then iterate over that array using array_keys on the original; anything which returns an array of length > 1 is a duplicate, and the result is the keys in which the duplicates are stored. Something like...
这将返回具有指定值的键数组,在您的情况下为 [0, 2]。如果您还想找到重复项,您可以先使用array_unique进行传递,然后使用 array_keys 对原始数组进行迭代;任何返回长度 > 1 的数组都是重复项,结果是存储重复项的键。就像是...
$uniqueKeys = array_unique($list[0])
foreach ($uniqueKeys as $uniqueKey)
{
$v = array_keys($list[0], $uniqueKey);
if (count($v) > 1)
{
foreach ($v as $key)
{
// Work with $list[0][$key]
}
}
}
回答by palindrom
In array_search()we can read:
在array_search()我们可以读到:
If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use
array_keys()with the optionalsearch_valueparameter instead.
如果在 haystack 中多次找到 Needle,则返回第一个匹配的键。要返回所有匹配值的键,请改用
array_keys()可选search_value参数。
回答by Till Theis
The following combination of function calls will give you all duplicate values:
以下函数调用组合将为您提供所有重复值:
$a = array(1, 1, 2, 3, 4, 5, 99, 2, 5, 2);
$unique = array_unique($a); // preserves keys
$diffkeys = array_diff_key($a, $unique);
$duplicates = array_unique($diffkeys);
echo 'Duplicates: ' . join(' ', $duplicates) . "\n"; // 1 2 5
回答by kenorb
You can achieve that using array_search()by using whileloop and the following workaround:
您可以array_search()通过使用while循环和以下解决方法来实现这一点:
while (($key = array_search("2009-09-09", $list[0])) !== FALSE) {
print($key);
unset($list[0][$key]);
}
Source: cue at openxbox at php.net
For one-multidimensional array, you may use the following function to achieve that (as alternative to array_keys()):
对于一维数组,您可以使用以下函数来实现(替代array_keys()):
function array_isearch($str, $array){
$found = array();
foreach ($array as $k => $v) {
if (strtolower($v) == strtolower($str)) {
$found[] = $k;
}
}
return $found;
}
Source: robertark, php.net
资料来源:robertark,php.net
回答by soulmerge
The PHP manual states in the Return Valuesection of the array_search()function documentationthat you can use array_keys()to accomplish this. You just need to provide the second parameter:
PHP 手册在函数文档的Return Value部分array_search()说明了您可以使用它array_keys()来完成此操作。您只需要提供第二个参数:
$keys = array_keys($list[0], "2009-09-09");

