Remove only numeric elements from an array

Asked

Viewed 292 times

4

I have a array in this way:

$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 numerical values 4, 7 and 7.5, and return this way below:

$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 elements of the numeric type of a array?

  • 2

    I did not want to post an answer talking about it, because it is not the case, but then do a search in function preg_grep. I like her because she returns the elements of array that match the past regular expression!

3 answers

6


You can use the 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 of the array. In the case of true it keeps. That way the remover_numero does the job of checking whether or not it is a number and then returning true if it is not numerical, or false if numerical.

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


Upshot:

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"
}

Test this!

  • It is legal way to solve my problem. Is there any alternative in this same line of reasoning not to remove values that are like string for example: "8"?

  • If you just want to remove it if it’s int or float may use the @Wallace Maxters, that removes everything that is not string. The is_numeric will remove even if it is string.

3

If in your case, the numbers that are present on array always are int or float (and not a number within a string), you can make it simpler yet:

$sem_numeros = array_filter($arr, 'is_string');

2

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

PS: If you don’t want to remove numeric strings from the array, such as: "42", "20"..., you will need to change the function is_numeric for is_int and is_float

function removeNumberFromArray(&$array) {

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

    $array = array_values($array);
}
  • 1

    Buddy, do you really think it’s necessary to write this much code to do a simple operation like this? PHP has some functions that can facilitate this type of operation. Using unset within a foreach may impair performance, depending on the size of the array.

  • It is not necessary, the other answers show this. But it has its didactic value. And it is always good to have alternative answers.

  • Yes, I agree with you Wallace, but if the beginner programmer doesn’t know how to remove the elements from the vector, he doesn’t know all the functions I addressed in my reply, such as: unset, is_numeric, is_int, is_float, array_values and also the passing of value by reference, not to mention that he will need to have a structured thought to organize all this. In short, this will all add more knowledge to it. And so it’s an alternative answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.