jeroenvanrensen's avatar

Refactor array loop in PHP

Hi,

I have this code:

protected function loopArray($array)
{
    $array = [1, 2, 3, 4, 5];

    foreach($array as $item) {
        $item++;
    }

    return $array;
}

How can I simplify this?

0 likes
3 replies
jeroenvanrensen's avatar

@sr57 Thanks! I knew there was something like that, but I couldn't think of the name.

@nakov Thanks, but I think the php array_map() function is better.

Please or to participate in this conversation.