Those tests are looping through 1 million records. If you're looping through 1 million records, you probably have much bigger problems than an addition second or two.
I prefer the map because its cleaner. If you're using collections, you can chain your collection methods together to make a much more expressive, readable piece of code than if you had three or four nested loops and a bunch of random arrays and counters keeping track of state for you.
There isn't much in it speed-wise between them - if it makes you code easier to read, then use them - if it's harder to read then don't :-) Sometimes I find the various array_* or laravel collections code harder to read (and much harder for non-laravel developers) - but sometimes it's cleaner. For instance :
// traditional php
$results = (some big array of things with a success flag);
$successfulResults = array();
foreach ($results as $result) {
if ($result['is_successful']) {
$successfulResults[] = $result;
}
}
... do something with the $successfulResults
// compared to (clearer imo)
$successfulResults = array_filter($results, function($result) {
return $result['is_successful'];
});
// or with a laravel collection
$successfulResults = $results->filter(function($result, $index) {
return $result['is_successful'];
}
Up to you if you find one cleaner/clearer really. I'm personally not a huge fan of lots of chained array/map operations, but to each their own :-) If your lead is telling you to use foreach for speed then I'd be giving them a bit of a side-eye stare, but if that's what the rest of your codebase uses then that's a way more valid argument imho :-)
A lot of folks that preach the no for loops mantra may be coming from the school of algorithms and/or other languages. In some languages, there are pre-compiled looping functions that already use the most efficient algorithms. So, you can be sure that your code is efficient when you used those functions because someone else already did the back end design work.
There's a whole science of function study that deals with algorithms and efficiency. Google Big O notation for more info. Here's a cheat sheet of different types of operations and efficiency - http://bigocheatsheet.com. Or, better yet, if you have some time to kill, try this free course on Coursera - https://www.coursera.org/learn/introduction-to-algorithms.
Just be careful what you use inside either the for loop or the anonymous function of array_map or array_filter. It can have a huge impact on efficiency if you nest loops.
For a simple for loop that isn't nested, as others have already stated, there's no real advantage other than readability. But sometimes, readability affects the money. There's always that to think about.