3

i have the following array

(
[0] => DHL - 4857998880
[1] => DHL - 4858005666
[2] => COA - 485344322
)

i want to loop through the array and if DHL found then i want to remove from the array. the numbers in front of DHL do not matter. any element with DHL in front i want to remove from the array.

i have created the following the regular expression to ignore the numbers in front but not sure how move forward from there.

foreach($result as $valDHL) {

   $s = preg_replace("/[^a-z-]/i", "", $valDHL);

}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

1

You can use array_filter to strip out the entries in your array that start with DHL, using the regex ^DHL to see if the entry starts with DHL:

$array = array(
0 => 'DHL - 4857998880',
1 => 'DHL - 4858005666',
2 => 'COA - 485344322'
);
$array = array_filter($array, function ($v) { return !preg_match('/^DHL/', $v); });
print_r($array);

Output:

Array (
  [2] => COA - 485344322 
)

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95