-1

i want to remove from array by minimum and maximum values
for example i have the next array

['10','11','12','12.5','13','14','15.5','16']

i need to remove values from 12 to 13 to be

['10','11','14','15.5','16']

how can make it working in PHP ?
can any one help ? thanks in advance.

Roufail
  • 551
  • 1
  • 8
  • 23

3 Answers3

2

You can loop through the array and use unset to remove the values that meet your condition, like this:

$values = ['10','11','12','12.5','13','14','15.5','16'];
foreach ($values as $i => $value) {
    if ($value >= 12 && $value <= 13) {
        unset($values[$i]);
    }
}

print_r($values); 

The result:

Array
(
    [0] => 10
    [1] => 11
    [5] => 14
    [6] => 15.5
    [7] => 16
)

You can also use array_filter function like this:

$values = ['10','11','12','12.5','13','14','15.5','16'];

$result = array_filter($values, function($value) {
                return $value < 12 || $value > 13;
            });

print_r($result); 
Claudio
  • 5,078
  • 1
  • 22
  • 33
0

Look this enter link description here

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )
Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.

and you should try it yourself

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Mar 02 '18 at 14:12
0

To Expand on @Philip Maurer

function removeBetween($arr, $min, $max){
          if($min>max){
               $tmp = $min;
               $min = $max;
               $max = $tmp;
          }

          $filteredArray = array_filter($array, function($value) {
              $value = (double)$value;
              return $value > $min || $value < $max;
          });
          return $filteredArray;
}

The if statement will flip the max and min values if the user entered them backwards. It makes it more generic because if you want to remove between 3 and 6 or 6 and 3 your result should be the same.

Also this is for an EXCLUSIVE method meaning the numbers you have entered are not included in the removal, if you wish to have them inclusive just add the equals sign in the return statement.

return $value > $min || $value < $max; would become return $value >= $min || $value <= $max;

Kevin Furlong
  • 27
  • 1
  • 9
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Mar 02 '18 at 14:13
  • fixed to add more detail, thank you – Kevin Furlong May 30 '18 at 17:51