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

后端 未结 9 1296
日久生厌
日久生厌 2020-11-30 00:09

I have an array that looks like this:

array(
  \'abc\' => 0,
  \'foo-bcd\' => 1,
  \'foo-def\' => 1,
  \'foo-xyz\' => 0,
  // ...
)
相关标签:
9条回答
  • 2020-11-30 00:38

    From PHP 5.3 you can use the preg_filter function: here

    $unprefixed_keys = preg_filter('/^foo-(.*)/', '$1', array_keys( $arr ));
    
    // Result:
    // $unprefixed_keys === array('bcd','def','xyz')
    
    0 讨论(0)
  • 2020-11-30 00:39

    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...
        }
    }
    
    0 讨论(0)
  • 2020-11-30 00:40
    $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);
    }
    
    0 讨论(0)
  • 2020-11-30 00:41

    Functional approach:

    Pick up an array_filter_key sort of function from the comments in http://php.net/array_filter or write your own. Then you could do:

    $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();
    }
    
    0 讨论(0)
  • 2020-11-30 00:46

    Simply I used array_filter function to achieve the solution as like follows

    <?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

    0 讨论(0)
  • 2020-11-30 00:50

    From PHP5.6, the array keys can be the sole subject of the filtration by using the ARRAY_FILTER_USE_KEY constant/flag.

    From PHP7.4, arrow functions make custom functions more concise and allow values to be passed into the custom function's scope without use().

    From PHP8, str_starts_with() can take the place of strpos(...) === 0

    Code: (Demo)

    $array = [
      'abc' => 0,
      'foo-bcd' => 1,
      'foo-def' => 1,
      'foo-xyz' => 0,
    ];
    
    $prefix = 'foo';
    
    var_export(
        array_filter(
            $array,
            fn($key) => str_starts_with($key, $prefix),
            ARRAY_FILTER_USE_KEY
        )
    );
    

    Output:

    array (
      'foo-bcd' => 1,
      'foo-def' => 1,
      'foo-xyz' => 0,
    )
    
    0 讨论(0)
提交回复
热议问题