-1

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

ajsie
  • 74,062
  • 99
  • 267
  • 376
  • What is the input array? What is the desired outout? What is the pattern? What have you tried? Too vague, no [mcve]. Unclear. – mickmackusa Jan 16 '22 at 07:49

2 Answers2

7

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);
user187291
  • 52,425
  • 19
  • 93
  • 127
3
foreach($array as $key => $value) {
    if(preg_match($pattern, $value)) {
        unset($array[$key]);
    }
}
chelmertz
  • 19,905
  • 4
  • 41
  • 46