How to filter values by keyword in array?

1

The codes below are not working very well the way I want:

$array = array('January', 'February', 'March', 'April', 'May', 'June');

function filterDataByValue(array $array, $value)
    {
        $filtered = array_filter($array, function ($var) use ($value) {
            return ($var !== $value);
        });

        return $filtered;
    }

   function filterDataByValues(array $array, array $values)
    {
        $filtered = array();
        if (count($values)) {
            foreach ($values as $k => $val) {
               $filtered[$k] = array_values(filterDataByValue($array, $val));

            }
        }
        return $filtered;
    }

    $data = array_values(filterDataByValues($array, array('January','April')));



    print_r($data);

I would like to filter a list of values and remove from my array. Obs, but not by the key, by the word. Example:

I have an array:

$frutas_origem = array('Pêssego', 'Maçã', 'Abacaxi', 'Morango');

Remove the words Apple and Strawberry:

$frutas_remocao = array('Maçã', 'Morango');

I have the result:

$frutas_resultado = array('Pêssego', 'Abacaxi');

The first method I posted removes the value, but what I want is to pass a list and remove the items, for example:

$frutas_resultado = metodoRemover($frutas_origem, $frutas_remocao); 
    
asked by anonymous 29.08.2017 / 00:25

1 answer

0

I was able to use array_diff ():

function filterDataByValues(array $array, array $values)
{
     $filtered = array_diff($array, $values);
     return $filtered;
}
    
29.08.2017 / 00:32