PHP - 从以特定字符串开头的数组中获取所有键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4979238/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:25:43  来源:igfitidea点击:

PHP - get all keys from a array that start with a certain string

phparraysarray-filter

提问by Alex

I have an array that looks like this:

我有一个看起来像这样的数组:

array(
  'abc' => 0,
  'foo-bcd' => 1,
  'foo-def' => 1,
  'foo-xyz' => 0,
  // ...
)

How can I get only the elements that start with foo-?

如何只获取以 开头的元素foo-

采纳答案by Developer-Sid

$arr_main_array = array('foo-test' => 123, 'other-test' => 456, 'foo-result' => 789);

foreach($arr_main_array as $key => $value){
    $exp_key = explode('-', $key);
    if($exp_key[0] == 'foo'){
         $arr_result[] = $value;
    }
}

if(isset($arr_result)){
    print_r($arr_result);
}

回答by erisco

Functional approach:

函数式方法:

Pick up an array_filter_keysort of function from the comments in http://php.net/array_filteror write your own. Then you could do:

array_filter_keyhttp://php.net/array_filter的评论中选择一种函数或编写自己的函数。那么你可以这样做:

$array = array_filter_key($array, function($key) {
    return strpos($key, 'foo-') === 0;
});

Procedural approach:

程序方法:

$only_foo = array();
foreach ($array as $key => $value) {
    if (strpos($key, 'foo-') === 0) {
        $only_foo[$key] = $value;
    }
}

Procedural approach using objects:

使用对象的过程方法:

$i = new ArrayIterator($array);
$only_foo = array();
while ($i->valid()) {
    if (strpos($i->key(), 'foo-') === 0) {
        $only_foo[$i->key()] = $i->current();
    }
    $i->next();
}

回答by Alex

This is how I would do it, though I can't give you a more efficient advice before understanding what you want to do with the values you get.

这就是我的做法,尽管在了解您想用所获得的值做什么之前,我无法给您更有效的建议。

$search = "foo-";
$search_length = strlen($search);
foreach ($array as $key => $value) {
    if (substr($key, 0, $search_length) == $search) {
        ...use the $value...
    }
}

回答by Suresh Velusamy

Simply I used array_filterfunction to achieve the solution as like follows

简单地我使用array_filter函数来实现解决方案如下

<?php

$input = array(
    'abc' => 0,
    'foo-bcd' => 1,
    'foo-def' => 1,
    'foo-xyz' => 0,
);

$filtered = array_filter($input, function ($key) {
    return strpos($key, 'foo-') === 0;
}, ARRAY_FILTER_USE_KEY);

print_r($filtered);

Output

输出

Array
(
    [foo-bcd] => 1
    [foo-def] => 1
    [foo-xyz] => 0
)

For live check https://3v4l.org/lJCse

实时检查https://3v4l.org/lJCse

回答by biziclop

From PHP 5.3 you can use the preg_filterfunction: here

从 PHP 5.3 开始,您可以使用该preg_filter函数:here

$unprefixed_keys = preg_filter('/^foo-(.*)/', '', array_keys( $arr ));

// Result:
// $unprefixed_keys === array('bcd','def','xyz')

回答by Tim Cooper

foreach($arr as $key => $value)
{
   if(preg_match('/^foo-/', $key))
   {
        // You can access $value or create a new array based off these values
   }
}

回答by Keyur

Modification to erisco's Functional approach,

修改erisco的功能方法,

array_filter($signatureData[0]["foo-"], function($k) {
    return strpos($k, 'foo-abc') === 0;
}, ARRAY_FILTER_USE_KEY);

this worked for me.

这对我有用。

回答by Netsurfer

In addition to @Suresh Velusamy's answer above (which needs at least PHP 5.6.0) you can use the following if you are on a prior version of PHP:

除了上面@Suresh Velusamy 的回答(至少需要 PHP 5.6.0)之外,如果您使用的是早期版本的 PHP,您还可以使用以下内容:

<?php

$input = array(
    'abc' => 0,
    'foo-bcd' => 1,
    'foo-def' => 1,
    'foo-xyz' => 0,
);

$filtered = array_filter(array_keys($input), function($key) {
    return strpos($key, 'foo-') === 0;
});

print_r($filtered);

/* Output:
Array
(
    [1] => foo-bcd
    [2] => foo-def
    [3] => foo-xyz
)
// the numerical array keys are the position in the original array!
*/

// if you want your array newly numbered just add:
$filtered = array_values($filtered);

print_r($filtered);

/* Output:
Array
(
    [0] => foo-bcd
    [1] => foo-def
    [2] => foo-xyz
)
*/