Filter a flat array by removing elements whose substring before the first occurring dot is found in a blacklist array
Asked Answered
T

4

6

I have an array that looks like this:

Array (
  [0] => Vice President
  [1] =>  
  [2] => other
  [3] => Treasurer
)

and I want delete the value with other in the value.

I tried to use array_filter() to filter this word, but array_filter() will delete all the empty values too.

I want the result to be like this:

Array (
  [0] => Vice President
  [1] =>  
  [2] => Treasurer
)

This is my PHP filter code:

function filter($element) {
    $bad_words = array('other');  

    list($name, $extension) = explode(".", $element);
    if (in_array($name, $bad_words))
        return;
    return $element;
}

$sport_level_new_arr = array_filter($sport_level_name_arr, "filter");

$sport_level_new_arr = array_values($sport_level_new_arr);

$sport_level_name = serialize($sport_level_new_arr);

Can I use another method to filter this word?

Transudate answered 1/7, 2011 at 11:43 Comment(2)
Relevant: Native function to filter array by prefix and Filter a flat array by another flat array containing regex patterns and Remove all elements from array that have a key that does not start with a certain stringTardiff
This question contains some dissonance because your sample data doesn't contain names and extensions AND your bad_words array only has one string in it. This has lead to answers which don't appear to be suitable for your code, but might satisfy your plain English expectations. In array_filter()'s callback, you don't retun the value, you return true or false. When you return the empty string as $element, array_filter() interprets that falsey value to mean that that element should be destroyed.Tardiff
H
4
foreach($sport_level_name_arr as $key => $value) {
  
  if(in_array($value, $bad_words)) {  
    unset($sport_level_name_arr[$key]);
  }

}
Hermy answered 1/7, 2011 at 11:47 Comment(2)
This doesn't renumber the array like the OP wanted.Cindycine
Hey EdoDodo Just need use php function "array_values",Transudate
E
9

array_filter() is the right function. Ensure your callback function has the correct logic.

Try the following:

function other_test($var) {
    // returns whether the value is 'other'
    return ($var != 'other');
}

$new_arr = array_filter($arr, 'other_test');

Note: if you want to reindex the array, then you could call $new_arr = array_values($new_arr); after the above.

Emmanuel answered 1/7, 2011 at 11:48 Comment(0)
H
4
foreach($sport_level_name_arr as $key => $value) {
  
  if(in_array($value, $bad_words)) {  
    unset($sport_level_name_arr[$key]);
  }

}
Hermy answered 1/7, 2011 at 11:47 Comment(2)
This doesn't renumber the array like the OP wanted.Cindycine
Hey EdoDodo Just need use php function "array_values",Transudate
P
2

This will create two arrays and will find the difference. In the second array we will put the elements to exclude:

array_values(array_diff($arr,array("other")));
Psych answered 1/7, 2011 at 11:49 Comment(2)
it will create two array and will find the diffrence, in the second array the we will put the elements to excludePsych
Notice that the asker's coding attempt is more nuanced. The required implementation needs to match the leading portion of each array value against an array of blacklisted prefixes.Tardiff
C
0

If the callback function returns true, the current value from input is returned into the result array. PHP Manual

So you need to do return true; in your filter(); function instead of return $element; to make sure that no empty values are removed.

Choate answered 1/7, 2011 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.