3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function one($s, $p) { return array_intersect_key($s, array_flip($p)); } function two($s, $p) { foreach($s as $key => $value) { if(!array_key_exists($key, $p)) { unset($s[$key]); } } return $s; } function benchmark(callable $function, $args=array(), $count=1) { $time = microtime(1); for($i=0; $i<$count; $i++) { $result = call_user_func_array($function, $args); } return [ 'total_time' => microtime(1) - $time, 'average_time' => (microtime(1) - $time)/$count, 'count' => $count ]; } $searchParameters = array( 'parameter1' => 'value1', 'parameter2' => 'value2', 'parameter3' => 'value3', ); $allowedParamters = array('parameter1','parameter2'); echo('array_intersect'.PHP_EOL); var_dump(benchmark('one', [$searchParameters, $allowedParamters], 1E6)); echo('foreach'.PHP_EOL); var_dump(benchmark('two', [$searchParameters, $allowedParamters], 1E6));
Output for 7.1.0
array_intersect array(3) { ["total_time"]=> float(0.27076101303101) ["average_time"]=> float(2.7076196670532E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(0.28002500534058) ["average_time"]=> float(2.8002595901489E-7) ["count"]=> float(1000000) }
Output for 7.0.14
array_intersect array(3) { ["total_time"]=> float(0.27657198905945) ["average_time"]=> float(2.7657198905945E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(0.2770049571991) ["average_time"]=> float(2.770049571991E-7) ["count"]=> float(1000000) }
Output for 5.4.45
array_intersect array(3) { ["total_time"]=> float(0.82319808006287) ["average_time"]=> float(8.2319903373718E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2938859462738) ["average_time"]=> float(1.2938868999481E-6) ["count"]=> float(1000000) }
Output for 5.4.41
array_intersect array(3) { ["total_time"]=> float(0.81947302818298) ["average_time"]=> float(8.194739818573E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.304426908493) ["average_time"]=> float(1.3044288158417E-6) ["count"]=> float(1000000) }
Output for 5.4.40
array_intersect array(3) { ["total_time"]=> float(0.8461492061615) ["average_time"]=> float(8.4615015983582E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2749931812286) ["average_time"]=> float(1.274994134903E-6) ["count"]=> float(1000000) }
Output for 5.4.39
array_intersect array(3) { ["total_time"]=> float(0.82516884803772) ["average_time"]=> float(8.2517004013062E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2866411209106) ["average_time"]=> float(1.286642074585E-6) ["count"]=> float(1000000) }
Output for 5.4.38
array_intersect array(3) { ["total_time"]=> float(0.83078217506409) ["average_time"]=> float(8.307831287384E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2921719551086) ["average_time"]=> float(1.2921731472015E-6) ["count"]=> float(1000000) }
Output for 5.4.34
array_intersect array(3) { ["total_time"]=> float(0.80664777755737) ["average_time"]=> float(8.0664896965027E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.3352379798889) ["average_time"]=> float(1.3352389335632E-6) ["count"]=> float(1000000) }
Output for 5.4.28
array_intersect array(3) { ["total_time"]=> float(0.80751395225525) ["average_time"]=> float(8.0751490592957E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2777328491211) ["average_time"]=> float(1.2777338027954E-6) ["count"]=> float(1000000) }
Output for 5.4.26
array_intersect array(3) { ["total_time"]=> float(0.85201811790466) ["average_time"]=> float(8.5201907157898E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2855861186981) ["average_time"]=> float(1.2855870723724E-6) ["count"]=> float(1000000) }
Output for 5.4.25
array_intersect array(3) { ["total_time"]=> float(0.85071611404419) ["average_time"]=> float(8.5071802139282E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2851660251617) ["average_time"]=> float(1.285168170929E-6) ["count"]=> float(1000000) }
Output for 5.4.24
array_intersect array(3) { ["total_time"]=> float(0.81980204582214) ["average_time"]=> float(8.1980299949646E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2876710891724) ["average_time"]=> float(1.287672996521E-6) ["count"]=> float(1000000) }
Output for 5.4.23
array_intersect array(3) { ["total_time"]=> float(0.81238007545471) ["average_time"]=> float(8.1238102912903E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2817089557648) ["average_time"]=> float(1.2817099094391E-6) ["count"]=> float(1000000) }
Output for 5.4.22
array_intersect array(3) { ["total_time"]=> float(0.82224488258362) ["average_time"]=> float(8.2224607467651E-7) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.21
array_intersect array(3) { ["total_time"]=> float(0.81421899795532) ["average_time"]=> float(8.1422019004822E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.3104839324951) ["average_time"]=> float(1.310485124588E-6) ["count"]=> float(1000000) }
Output for 5.4.20
array_intersect array(3) { ["total_time"]=> float(1.50501704216) ["average_time"]=> float(1.5050179958344E-6) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.19
array_intersect array(3) { ["total_time"]=> float(1.5181469917297) ["average_time"]=> float(1.5181479454041E-6) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.18
array_intersect array(3) { ["total_time"]=> float(0.82715010643005) ["average_time"]=> float(8.2715106010437E-7) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.17
array_intersect array(3) { ["total_time"]=> float(0.82989692687988) ["average_time"]=> float(8.298978805542E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2871098518372) ["average_time"]=> float(1.2871110439301E-6) ["count"]=> float(1000000) }
Output for 5.4.16
array_intersect array(3) { ["total_time"]=> float(0.82500004768372) ["average_time"]=> float(8.2500100135803E-7) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.15
array_intersect array(3) { ["total_time"]=> float(0.82927989959717) ["average_time"]=> float(8.2928085327148E-7) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.14
array_intersect array(3) { ["total_time"]=> float(0.87230181694031) ["average_time"]=> float(8.723030090332E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2893779277802) ["average_time"]=> float(1.2893788814545E-6) ["count"]=> float(1000000) }
Output for 5.4.13
array_intersect array(3) { ["total_time"]=> float(0.84872889518738) ["average_time"]=> float(8.4872984886169E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2883448600769) ["average_time"]=> float(1.2883458137512E-6) ["count"]=> float(1000000) }
Output for 5.4.12
array_intersect array(3) { ["total_time"]=> float(0.8516218662262) ["average_time"]=> float(8.5162305831909E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.3259239196777) ["average_time"]=> float(1.3259248733521E-6) ["count"]=> float(1000000) }
Output for 5.4.11
array_intersect array(3) { ["total_time"]=> float(0.81964588165283) ["average_time"]=> float(8.1964707374573E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.353716135025) ["average_time"]=> float(1.3537170886993E-6) ["count"]=> float(1000000) }
Output for 5.4.10
array_intersect array(3) { ["total_time"]=> float(1.3932199478149) ["average_time"]=> float(1.3932218551636E-6) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.9
array_intersect array(3) { ["total_time"]=> float(0.98619985580444) ["average_time"]=> float(9.8620080947876E-7) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.8
array_intersect array(3) { ["total_time"]=> float(0.81381797790527) ["average_time"]=> float(8.1381893157959E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2738571166992) ["average_time"]=> float(1.2738580703735E-6) ["count"]=> float(1000000) }
Output for 5.4.7
array_intersect array(3) { ["total_time"]=> float(0.80224895477295) ["average_time"]=> float(8.0225014686584E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2646939754486) ["average_time"]=> float(1.2646949291229E-6) ["count"]=> float(1000000) }
Output for 5.4.6
array_intersect array(3) { ["total_time"]=> float(1.4959650039673) ["average_time"]=> float(1.4959661960602E-6) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.5
array_intersect array(3) { ["total_time"]=> float(0.812096118927) ["average_time"]=> float(8.1209707260132E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2825520038605) ["average_time"]=> float(1.2825531959534E-6) ["count"]=> float(1000000) }
Output for 5.4.4
array_intersect array(3) { ["total_time"]=> float(0.83132815361023) ["average_time"]=> float(8.3132910728455E-7) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.3
array_intersect array(3) { ["total_time"]=> float(0.82119393348694) ["average_time"]=> float(8.2119393348694E-7) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.2
array_intersect array(3) { ["total_time"]=> float(0.82360792160034) ["average_time"]=> float(8.2360887527466E-7) ["count"]=> float(1000000) } foreach array(3) { ["total_time"]=> float(1.2814450263977) ["average_time"]=> float(1.281445980072E-6) ["count"]=> float(1000000) }
Output for 5.4.1
array_intersect array(3) { ["total_time"]=> float(0.81997203826904) ["average_time"]=> float(8.1997299194336E-7) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.4.0
array_intersect array(3) { ["total_time"]=> float(1.2669107913971) ["average_time"]=> float(1.26691198349E-6) ["count"]=> float(1000000) } foreach
Process exited with code 137.
Output for 5.3.0 - 5.3.28
Parse error: syntax error, unexpected '[' in /in/RUjPB on line 26
Process exited with code 255.