0

Is there a way to eliminate more than one items from array without looping through it ?

Eg: array(1,3,67, 78, 60 , 5, 34, 68); 

I want to remove items > 50 at once

3
  • Why don't you want to use a loop? Commented Nov 12, 2014 at 9:02
  • Because when there are many items, it will slow down the process Commented Nov 12, 2014 at 9:04
  • It would require thousands if not more to slow your process. Commented Nov 12, 2014 at 9:09

2 Answers 2

4

Sure, you can use array_filter:

$array = array_filter(array(1, 3, 67, 78, 60 , 5, 34, 68), function($element) {
                                                              return $element <= 50;
                                                           });

The callback function must return true for those items in the array that you want to keep.

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

Comments

3

This is not possible to do without looping, however you can use array_filter() function in order to hide the loop

function remove($var) { return $var < 50; }
$data = array_filter($data, 'remove');

Comments

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.