2

I usually use foreach loop to remove some items from array.

foreach($array as $key=>$item){
    if($item == 'something'){
        unset($array[$key]);
    }
}

Is this possible to remove items via array_walk or array_filter?

0

1 Answer 1

9
$newarray = array_filter($array, function($var) {
    return ($var != 'something');
});
Sign up to request clarification or add additional context in comments.

1 Comment

With the advent of PHP 5.6.0, it's also possible to filter by key with $newarray = array_filter($array, function($key) { return preg_match('/something/', $key); }, ARRAY_FILTER_USE_KEY); or function($value, $key) with the ARRAY_FILTER_USE_BOTH flag if the condition is looking at both key and value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.