Reputation: 577
Suppose $arr is this:
Array
(
[0] => 43
[1] => 120
[2] => 5
[3] => 5465
[4] => 397
)
and I want any values between 111-5000 to be removed. If done correctly, my array would be:
Array
(
[0] => 43
[1] => 5
[2] => 5465
)
Hopefully the keys update on their own. Any ideas on how to accomplish that?
Upvotes: 1
Views: 48
Reputation: 36
You can achieve using the following logic too.
foreach ($array as $key => $value) {
if($value >= 111 && $value <= 5000) {
unset($array[$key]);
}
}
But what John mentioned in the above post is the optimal solution. It's quicker than this logic.
Thanks!
Upvotes: 1
Reputation: 219804
Use array_filter()
with a custom callback that removes unwanted values:
$array = array(43,120,5,5465,397);
$filtered = array_filter($array, function($val){
return ($val < 111 || $val > 5000);
});
print_r($filtered); // Array ( [0] => 43 [2] => 5 [3] => 5465 )
You can also do
$array = array(43,120,5,5465,397);
$filtered = array_filter($array, function($val){
return !($val > 111 && $val < 5000);
});
Upvotes: 4