As suggested by Anderson Carlos Woss in the comments, the function:
array_filter ( array $array \[, callable $callback \[, int $flag = 0 \]\] ) : array
Filter elements of an array using a callback function. Iterate over each array value by passing them to the callback function. If the callback function returns true, the current array value is returned in the result matrix.
<?php
$numeros = [-25, -16, -9, -4, 0 , 4 , 9 , 16, 25];
echo "Números:" . PHP_EOL;
print_r($numeros);
// Filtra os valores maiores que 0
$positivos = array_filter($numeros, function ($val) {return $val > 0;} );
echo "Números positivos:" . PHP_EOL;
print_r($positivos);
// Aplica a função sqrt(), raíz quadrada, aos valores positivos previamente filtrados
$raizes = array_map('sqrt', $positivos );
echo "Raízes dos números positivos:" . PHP_EOL;
print_r($raizes);
Repl.it
Search by array_filter
– Woss
array_filter
seems to me quite unnecessary in something so simple, usually it can be solved within the samefor...
if the intention is not to reuse the array for something else, for example save this array in a file, database or other format (such as an xml, summarizing, save the data)in practice will almost always be use of unnecessary resource to perform something that often ends up being executed twice. Of course I’m not saying that the function is useless, I’m saying that the use of it for things like this sometimes ends up being an exaggeration.– Guilherme Nascimento