I need to change the array, I know that the conditions use the filter function, But the array scheme is such that I want to use the map function.

Does the condition use map functions?

  • In map, it is impossible to filter the elements, there you need to return something else, item will be null. - Ilya Zelenko

1 answer 1

As array_map as I know, a Collection map is an analogue of array_map .

http://php.net/manual/ru/function.array-map.php

Immediately see:

array_map - Applies a callback function to all elements of the specified arrays

Those. if you want to filter the array, it will not work, if you want to apply something to each element (change them), you can and if can be used here.

 function func($v) { if ($v == 1) { return 'first'; } else { return 'second'; } } $a = array(1, 2); var_dump(array_map("func", $a)); 

"func" - can be replaced with an anonymous function, and there you can also use this whole thing.


It should be understood that array_map and array_filter are 2 completely different functions created for different needs.