【问题标题】:selecting an array key based on partial string根据部分字符串选择数组键
【发布时间】:2011-04-25 07:49:16
【问题描述】:

我有一个数组,在那个数组中我有一个数组键,看起来像 show_me_160 这个数组键可能会发生一点变化,所以有时页面可能会加载并且数组键可能是 show_me_120,我现在想要是否可以只匹配数组键直到最后一个 _ 以便我可以检查最后一个下划线之后的值是什么?

【问题讨论】:

  • 你能举个例子吗?
  • 使用 array_map 和回调函数 preg_metch 为#show_me_([\d])+#

标签: php arrays string substring strpos


【解决方案1】:

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

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

【讨论】:

  • 我认为这是我正在处理的工作,但我怀疑它在大型阵列上会非常缓慢。
【解决方案2】:

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

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

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

【讨论】:

  • 这适用于我的情况,但请注意这一行:if (strpos($k, 'show_me_') !== false) 应该是 if (strpos($v, 'show_me_') !== false)
  • 最好使用0 === strpos($k, 'show_me_') 来确保它只是匹配的前缀,而noshow_me_ 之类的值也不匹配
【解决方案3】:

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

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

【讨论】:

  • 您甚至可以使用\K 来避开捕获组。
【解决方案4】:

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

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

【讨论】:

    【解决方案5】:
     foreach($myarray as $key=>$value)
        if(count(explode('show_me_',$event_key)) > 1){
             //if array key contains show_me_
        }
    

    更多信息(示例):

    如果数组键包含“show_me_”

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

    print_r($example)

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

    print_r(count($example))

    2 
    

    print_r($example[1])

     120 
    

    【讨论】:

    • 您能否更详细地解释一下这段代码的含义?
    • 如果数组键包含'show_me_' $example = explode('show_me_','show_me_120'); $example 是:数组 ( [0] => [1] => 120 ) count($example) 是:2 $example[1] 是:120跨度>
    • 如果可以使用 substring 或 strpos,为什么还需要两个函数(explode 和 count)?
    【解决方案6】:

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

    return array 'searched_key' => '分配给键的值'

    【讨论】:

    • 没有上下文的未格式化代码块通常不是一个好的答案。也许edit 是答案,并清楚说明您的建议以及您认为它回答问题的原因。
    • 开发者如何知道这两个变量:'searched_key' & 'value assignment to the key' ??此响应的格式也不正确
    【解决方案7】:

    要在数组键中搜索特定字符串,您可以使用 array_filter(); see docs

    // 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);
    

    如果您在数组值中搜索,您可以使用标志 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
    );
    

    或

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

    如果您在数组值和数组键中搜索,您可以使用标志 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
    );
    

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

    【讨论】:

    • 如果我想在这个逻辑的末尾返回字符串值怎么办?因为目前这是在一个数组中返回键和值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多