PHP - Is array_map faster than foreach?

i am here now 'cause i was googleize the words : "php 8 foreach most fastest" and do not threat his question as duplicate please 'cause i wrote in all php versions the fastest php framework in the world(classic structural,oop , oop at sum based static public functions) and in particular in the php 7 the array_filter i every time use was fastest in many cases than foreach ,now i use in xampp windows 10 x64 the same methods to benchmark and i got fastest results on foreach. so the answer on your question is YES INDEED IS FOREACH FASTEST so use base that in php 8 and the other code in php 7 or less !!!

here is the versions of thecode i test(i was modify a simple script to popup all new bootstrap icons 1.4.1 on the screen and):

the first code i test and if you uncoment parts of it and the correct parts for combining you got some little differences but idea remain the same

 $icons.=<<<icon
    {$pa} {$pa2} 
    icon;
    
    /*
    $icons.=<<<icon
    {$pa}
    icon;
    */
    
    //});
    //},ARRAY_FILTER_USE_KEY);
    },ARRAY_FILTER_USE_BOTH);
    print_r($icons);
    //0.00001200

the equivalent code i test

$icons='';
    foreach([0=>1,1=>99] as $ch => $vl){
    $icons.=<<<icon
    {$ch} {$vl} 
    icon;
    }
    print_r($icons);
    //0.00000694

i bench it with a "plugin" improvisation =a cli php code called with a .exe purebasic from inside a editor doesn't have plugin possibilities( editplus ) but have user text filters so you see at the end of each script the result of the php script:

  if ($argv[1]=='bench'){
            $cmd=cmd(1);
            ob_start();
            $start= microtime(true);
            for($i=0;$i<10000;$i++) eval($cmd);
            $stop= microtime(true);
            ob_clean ();
            echo PHP_EOL.'//'. number_format(($stop-$start)/10000, 8, '.', '');       
    }

I am a public static function "consumer" too ! cheers!


I tested this on a Symfony project just now, had to Google because it seems so significant. Script went from 160ms using foreach() to 260ms using array_map(). Considering the size of the application thats quite a large increase from a single method call.


For the record (php 7.4 + 64bits + windows)

https://github.com/EFTEC/php-benchmarks/blob/master/benchmark_arraymap_foreach.php

  • foreach = 0.10213899612427
  • array_map = 0.18259811401367
  • array_map (static) = 0.18230390548706
  • array_map (calling a function) = 0.17731499671936

Foreach is still faster but also if we use a static function or not, it doesn't make any difference:

    $result = array_map(function ($number) {
        return $number * 10;
    }, $numbers);
    $result = array_map(static function ($number) {
        return $number * 10;
    }, $numbers);

I believe this answers your question and is current as of 2015-01-22

Performance of foreach, array_map with lambda and array_map with static function

array_map although more elegant is sadly slower in PHP. Particularly if using it with a closure.