1
implementing in my project the user solution Augusto Vasques in the matter :
How to remove an array from occurrence found within a sub array!
I came across the situation of not being able to search for values stored in variables, only works by inserting strings directly! Follows code.
$arr_val=array('abacaxi','limão');
// array para ser modificado
$frutas = [
['maça', 1256],
['abacaxi', 1234],
['pera', 235],
['laranja', 2135],
['limão', 2315],
['morango', 2351]
];
//laço para testar cada valor do array "$arr_val" e remover
foreach($arr_val as $val_busca){
$frutas = array_filter($frutas, function ($item){
// aqui o valor de cada fruta a ser encontrado "$val_busca" não funciona por variavél
return !in_array($val_busca, $item);
});
}
echo "<pre>";print_r($frutas);echo"</pre>";
I thank you all.
correct, setting $val_search as global within the scope solved the problem!
– Caio Lourençon
@Caiolourençon this approach is more efficient than declaring a global because in the example yes a global works, but in the future you want to encapsulate this procedure in a function/method or turn the filter into an asynchronous code the global ceases to work.
– Augusto Vasques
Blz, I’ll take recommendations.
– Caio Lourençon