3 ms·
Doch. Try piping the result of array_filter to array_map sometime. You can’t do it without an intermediate call to array_values first because the result of pass
by ashton314 6y ago
Doch. Try piping the result of array_filter to array_map sometime. You can’t do it without an intermediate call to array_values first because the result of passing a list to array_filter does not behave like a list anymore: it operates strictly like a map and cannot have map called on it. (Oh the irony!)
- zerocrates 6y agoThe caveat I'm aware of with array_filter is that it can leave gaps in the indexes, but having an array like that doesn't break array_map. The output from it will just have the same index gap as the input.
- TazeTSchnitzel 6y agoYeah, there's nothing magical here: $ php -r 'var_dump([1, 2, 3, 4]);' array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) } $ php -r 'var_dump(array_filter([1, 2, 3, 4], function ($x){return (bool)($x % 2);}));' array(2) { [0]=> int(1) [2]=> int(3) } $ php -r 'var_dump(array_map("strval", array_filter([1, 2, 3, 4], function ($x){return (bool)($x % 2);})));' array(2) { [0]=> string(1) "1" [2]=> string(1) "3" }
- deleted 6y ago[deleted]