Remove only numeric elements from an array

4

I have an array like this:

$arr = array('prolongar-se', 'durante, 4, 'a', 7, 'dias', 
    'dependendo', 'da', 'dose', 'administrada', 'e', 'do', 
    'pH', 'da', 'água', 'sendo', 7.5, 'ml', 'por','cada','paciente');

I would like to remove the numeric values 4 , 7 and 7.5 , and return it as follows:

$arr = array('prolongar-se', 'durante, 'a', 'dias', 'dependendo', 
    'da', 'dose', 'administrada', 'e', 'do', 'pH', 'da', 'água', 
    'sendo', 'ml', 'por','cada','paciente');

How can I remove numeric type elements from an array ?

    
asked by anonymous 04.03.2017 / 20:55

3 answers

6

You can use array_filter() , for example:

$arr = array('prolongar-se', 'durante', 4, 'a', 7, 'dias', 
    'dependendo', 'da', 'dose', 'administrada', 'e', 'do', 
    'pH', 'da', 'água', 'sendo', 7.5, 'ml', 'por','cada','paciente');

function remover_numero($string) {

    return !is_numeric($string);

}

$arr = array_filter($arr, 'remover_numero');

The array_filter expects a return of true or false , in the case of false it removes the value from the array. In the case of true it keeps. This way remover_numero does the job of checking whether or not it is a number and then return true if it is not numeric or false if it is numeric.

You can also use something like: array_filter($arr, function($string) { return !is_numeric($string); }) that will have the same effect.

Result:

array(18) {
  [0]=>
  string(12) "prolongar-se"
  [1]=>
  string(7) "durante"
  [3]=>
  string(1) "a"
  [5]=>
  string(4) "dias"
  [6]=>
  string(10) "dependendo"
  [7]=>
  string(2) "da"
  [8]=>
  string(4) "dose"
  [9]=>
  string(12) "administrada"
  [10]=>
  string(1) "e"
  [11]=>
  string(2) "do"
  [12]=>
  string(2) "pH"
  [13]=>
  string(2) "da"
  [14]=>
  string(5) "água"
  [15]=>
  string(5) "sendo"
  [17]=>
  string(2) "ml"
  [18]=>
  string(3) "por"
  [19]=>
  string(4) "cada"
  [20]=>
  string(8) "paciente"
}

Try This!

    
04.03.2017 / 21:48
3

If in your case, the numbers that are present in array are always int or float (not a number within a string ), you can do even simpler:

$sem_numeros = array_filter($arr, 'is_string');
    
04.03.2017 / 23:18
2

You can use php's unset function to remove elements from the vector, but before removing, you need to check if the element is a number, a numeric string or just a string, so you can use the is_numeric and finally, you need to" reindex "the vector, since the unset removes an element from the vector and leaves a" hole "for it, you can use the function array_values that will return all values of the vector and will index it numerically.

PS: If you do not want to remove vector strings such as: "42", "20" ..., you need to change the is_numeric by is_int and is_float

function removeNumberFromArray(&$array) {

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

    $array = array_values($array);
}
    
04.03.2017 / 21:42