php 根据部分字符串选择数组键

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

selecting an array key based on partial string

phparraysstringsubstringstrpos

提问by sea_1987

I have an array and in that array I have an array key that looks like, show_me_160this array key may change a little, so sometimes the page may load and the array key maybe show_me_120, I want to now is possible to just string match the array key up until the last _so that I can check what the value is after the last underscore?

我有一个数组,在该数组中我有一个数组键,看起来像show_me_160这个数组键可能会发生一些变化,所以有时页面可能会加载,而数组键可能会加载show_me_120,我希望现在可以只字符串匹配数组键直到最后一个,_以便我可以检查最后一个下划线后的值是多少?

采纳答案by oezi

one solution i can think of:

我能想到的一种解决方案:

foreach($myarray as $key=>$value){
  if("show_me_" == substr($key,0,8)){
    $number = substr($key,strrpos($key,'_'));
    // do whatever you need to with $number...
  }
}

回答by Ian

I ran into a similar problem recently. This is what I came up with:

我最近遇到了类似的问题。这就是我想出的:

$value = $my_array[current(preg_grep('/^show_me_/', array_keys($my_array)))];

回答by Geoffrey Bachelet

you would have to iterate over your array to check each key separately, since you don't have the possibility to query the array directly (I'm assuming the array also holds totally unrelated keys, but you can skip the ifpart if that's not the case):

您必须遍历数组以分别检查每个键,因为您无法直接查询数组(我假设该数组也包含完全不相关的键,但if如果不是,您可以跳过该部分案件):

foreach($array as $k => $v)
{
  if (strpos($k, 'show_me_') !== false)
  {
    $number = substr($k, strrpos($k, '_'));
  }
}

However, this sounds like a very strange way of storing data, and if I were you, I'd check if there's not an other way (more efficient) of passing data around in your application ;)

但是,这听起来像是一种非常奇怪的数据存储方式,如果我是您,我会检查是否没有其他方式(更有效)在您的应用程序中传递数据;)

回答by codaddict

You can also use a preg_matchbased solution:

您还可以使用preg_match基于解决方案:

foreach($array as $str) {
        if(preg_match('/^show_me_(\d+)$/',$str,$m)) {
                echo "Array element ",$str," matched and number = ",$m[1],"\n";
        }
}

回答by MidAde

to search for certain string in array keys you can use array_filter();see docs

要在数组键中搜索某些字符串,您可以使用array_filter();查看文档

// the array you'll search in
$array = ["search_1"=>"value1","search_2"=>"value2","not_search"=>"value3"];
// filter the array and assign the returned array to variable
$foo = array_filter(
    // the array you wanna search in
    $array, 
    // callback function to search for certain sting
    function ($key){ 
        return(strpos($key,'search_') !== false);
    }, 
    // flag to let the array_filter(); know that you deal with array keys
    ARRAY_FILTER_USE_KEY
);
// print out the returned array
print_r($foo);

if you search in the array values you can use the flag 0 or leave the flag empty

如果您在数组值中搜索,您可以使用标志 0 或将标志留空

$foo = array_filter(
    // the array you wanna search in
    $array, 
    // callback function to search for certain sting
    function ($value){ 
        return(strpos($value,'value') !== false);
    }, 
    // flag to let the array_filter(); know that you deal with array value
    0
);

or

或者

$foo = array_filter(
    // the array you wanna search in
    $array, 
    // callback function to search for certain sting
    function ($value){ 
        return(strpos($value,'value') !== false);
    }
);

if you search in the array values and array keys you can use the flag ARRAY_FILTER_USE_BOTH

如果您在数组值和数组键中搜索,您可以使用标志 ARRAY_FILTER_USE_BOTH

$foo = array_filter(
    // the array you wanna search in
    $array, 
    // callback function to search for certain sting
    function ($value, $key){ 
        return(strpos($key,'search_') !== false or strpos($value,'value') !== false);
    },
    ARRAY_FILTER_USE_BOTH
);

in case you'll search for both you have to pass 2 arguments to the callback function

如果您要搜索两者,则必须将 2 个参数传递给回调函数

回答by Aran

I recently had a similar challenge where I needed to scan a large multi-dimensional array for keys with a certain prefix so created a couple of functions for searching recursively for keys both case-sensitive and case-insensitively.

我最近遇到了一个类似的挑战,我需要扫描一个大型多维数组以查找具有特定前缀的键,因此创建了几个函数来递归搜索区分大小写和不区分大小写的键。

I've put these up on Github at the link below:

我已将这些放在 Github 上,链接如下:

https://gist.github.com/aran112000/c77a8d8f4c41af6f5800b485caf94fa9

https://gist.github.com/aran112000/c77a8d8f4c41af6f5800b485caf94fa9

回答by user5510975

 foreach($myarray as $key=>$value)
    if(count(explode('show_me_',$event_key)) > 1){
         //if array key contains show_me_
    }

More information (example):

更多信息(示例):

if array key contain 'show_me_'

如果数组键包含“show_me_”

$example = explode('show_me_','show_me_120');

$example = expand('show_me_','show_me_120');

print_r($example)

打印_r($示例)

Array ( [0] => [1] => 120 ) 

print_r(count($example))

打印_r(计数($示例))

2 

print_r($example[1])

打印_r($example[1])

 120 

回答by qdras

filter_array($array,function ($var){return(strpos($var,'searched_word')!==FALSE);},);

filter_array($array,function ($var){return(strpos($var,'searched_word')!==FALSE);},);

return array 'searched_key' => 'value assigned to the key'

返回数组 'searched_key' => '分配给键的值'