PhpStorm 2021.1 Help

Code Inspection: 'array_map' call can be converted to loop

Reports the array_map calls that can be replaced with the foreach loop.

The array_map (php.net) function accepts one or several arrays as arguments, applies a callback function to each of their elements, and returns a new array containing the resulting elements. You can also use a foreach loop (php.net) to achieve the same result.

In the following example, the myArr array's values are doubled by using the odd callback function. The function is called from either the array_filter function call or the foreach loop.

function double($var) { return $var * 2; } $myArr = [1, 2, 3, 4, 5, 6]; $doubledValues = array_map('double', $myArr);
function double($var) { return $var * 2; } $myArr = [1, 2, 3, 4, 5, 6]; $array_map = []; foreach ($myArr as $key => $var) { $array_map[$key] = double($myArr[$key]); } $doubledValues = $array_map;

Suppress an inspection in the editor

  1. Position the caret at the highlighted line and press Alt+Enter or click the Intention action icon.

  2. Click the arrow next to the inspection you want to suppress and select the necessary suppress action.

Last modified: 16 July 2021