php - 在数组中查找匹配模式的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12482388/
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
php - finding keys in an array that match a pattern
提问by dot
I have an array that looks like:
我有一个看起来像的数组:
Array ( [2.5] => ABDE [4.8] => Some other value )
How would I find any key/value pair where the key matches a pattern? I will know the value of the first digit in the key,but not the second. so for example, using a prefix of "2.", I want to somehow be able to find the key "2.5" and return both the key and the value "ABDE".
我如何找到键匹配模式的任何键/值对?我会知道密钥中第一个数字的值,但不会知道第二个数字的值。例如,使用前缀“2.”,我希望能够以某种方式找到键“2.5”并返回键和值“ABDE”。
I was thinking about using a regular expression with a pattern like:
我正在考虑使用具有以下模式的正则表达式:
$prefix = 2;
$pattern = '/'.$prefix.'\.\d/i';
and then looping through the array and checking each key. (by the way, just for demo purposes, $prefix has been hardcoded to 2, but in the real system, this is a value provided by the user's input).
然后遍历数组并检查每个键。(顺便说一下,只是为了演示目的,$prefix 已经硬编码为 2,但在实际系统中,这是由用户输入提供的值)。
I'm wondering if there's a simpler way to do this?
我想知道是否有更简单的方法来做到这一点?
Thanks.
谢谢。
采纳答案by Ibu
you can simply loop through the array and check the keys
您可以简单地遍历数组并检查键
$array = array(...your values...);
foreach($array as $key => $value) {
if (preg_match($pattern,$key)){
// it matches
}
}
You can wrap it in a function and pass your pattern as parameter
您可以将它包装在一个函数中并将您的模式作为参数传递
回答by JvdBerg
I think you need something like this:
我认为你需要这样的东西:
$keys = array_keys($array);
$result = preg_grep($pattern, $keys);
The result will be a array that holds all the keys that match the regex. The keys can be used to retrieve the corresponding value.
结果将是一个包含与正则表达式匹配的所有键的数组。这些键可用于检索相应的值。
Have a look at the preg_grepfunction.
看看preg_grep函数。
回答by Nick B
Old question, but here's what I like to do:
老问题,但这是我喜欢做的:
$array = [ '2.5' => 'ABDE', '4.8' => 'Some other value' ];
preg_grep+ array_keyswill find all keys
preg_grep+array_keys会找到所有的键
$keys = preg_grep( '/^2\.\d/i', array_keys( $array ) );
You can add array_intersect_keyand array_flipto extract a slice of the array that matches the pattern
您可以添加array_intersect_key和array_flip提取与模式匹配的数组切片
$vals = array_intersect_key( $array, array_flip( preg_grep( '/^2\.\d/i', array_keys( $array ) ) ) );
回答by Alex
That are my ways
那是我的方式
$data = ["path"=>"folder","filename"=>"folder/file.txt","required"=>false];
// FIRST WAY
$keys = array_keys($data);
if (!in_array("path", $keys) && !in_array("filename",$keys) && !in_array("required",$keys)) {
return myReturn(false, "Dados pendentes");
}
// SECOND WAY
$keys = implode("," array_keys($data));
if (!preg_match('/(path)|(filename)|(required)/'), $keys) {
return myReturn(false, "Dados pendentes");
}
回答by Primigenius
To get just the part of the array with matching keys you could also write
要仅获取具有匹配键的数组部分,您还可以编写
$matching_array = array_flip(preg_grep($pattern, array_flip($your_array)));
This might just lead to problems performance-wise, if your array gets too big.
如果您的阵列变得太大,这可能只会导致性能方面的问题。
回答by Turdaliev Nursultan
For future programmers who encounter the same issue. Here is a more complete solution which doesn't use any loops.
对于遇到相同问题的未来程序员。这是一个更完整的解决方案,它不使用任何循环。
$array = ['2.5'=> 'ABCDE', '2.9'=>'QWERTY'];
$keys = array_keys($array);
$matchingKeys = preg_grep('/^2\.+/', $keys);
$filteredArray = array_intersect_key($array, array_flip($matchingKeys));
print_r($filteredArray);

