remove a key if value matches a pattern?

前端 未结 2 1948
小鲜肉
小鲜肉 2020-12-22 09:57

i\'ve got an array with string values.

i want to search for a pattern with regex and if matched, remove the key containing the value.

how would i accomplish

相关标签:
2条回答
  • 2020-12-22 10:37

    preg_grep: http://php.net/manual/en/function.preg-grep.php

    $a = array('foo' => 'xx', 'bar' => '12');
    $b = preg_grep('~[a-z]~', $a, PREG_GREP_INVERT);
    print_r($b);
    
    0 讨论(0)
  • 2020-12-22 11:03
    foreach($array as $key => $value) {
        if(preg_match($pattern, $value)) {
            unset($array[$key]);
        }
    }
    
    0 讨论(0)
提交回复
热议问题