0

i have the following array

[0] => 1009
[1] => 1033
[2] => 2052
[3] => ANNEX 4E
[4] => Bill of Lading
[5] => CERTIFICATE OF ANALYSIS
[6] => CERTIFICATE OF ORIGIN
[7] => DHL - 5130431633

i want to remove the value with DHL followed by any number. any value containing DHL i want to remove.

for example DHL - 5130431633 or DHL - 51304345654 should be removed number in front of the dhl do not matter

i have use the following code but it do not work

$xmlarrayresultdocsave = preg_grep("/^DHL$/i", $xmlarrayresultdocsave, 
PREG_GREP_INVERT);
0

2 Answers 2

1

Your regex looks for DHL exclusively on the line, and not the entire string. You'll want to do:

/^DHL.*$/i

Which matches DHL and everything else until the end of the line.

Sign up to request clarification or add additional context in comments.

Comments

0

The following code will do exactly what you want.

$haystack = array(
    'Horse',
    'DHL',
    'DHL - 123',
    'Pedestrian',
    'Philosopher',
    'DHL-123',
    'DHL - 123'
);

$needle = 'DHL - ';

// Find all values with DHL
$test = array_filter($haystack, function($el) use ($needle) {
        return (strpos($el, $needle) !== false);
});

// Unset the values
foreach (array_keys($foundRows) as $key) {
    unset($haystack[$key]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.