Does the array_filter
function change the given array in place or return a new array? It's not clear to me from the documentation.
TLDR: Cal is right! array_filter
returns a new array, leaving the original unmodified.
I agree that the documentation for array_filter
is unclear. For folks looking for a quick clarification in the future, here is the code and result of the test I wrote to verify.
code:
<?php
$testArray = [1,2,3,4,5,6,7,8,9,10];
$filteredArray = array_filter($testArray, function($k) {
return $k % 2 === 0;
});
print_r($testArray);
print_r($filteredArray);
result:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
Array
(
[1] => 2
[3] => 4
[5] => 6
[7] => 8
[9] => 10
)