3v4l.org

run code in 300+ PHP versions simultaneously
<?php $allTheThings = [ ['stuff' => [ ['id' => 42], ]], ['stuff' => [ ['id' => 'never'], ]], ['stuff' => [ ['id' => 'gonna'], ]], ['stuff' => [ ['id' => 'give'], ]], ['stuff' => [ ['id' => 'you'], ]], ['stuff' => [ ['id' => 'up'], ]], ['stuff' => [ ['id' => 42], ]], ['stuff' => [ ['id' => 'never'], ]], ['stuff' => [ ['id' => 'gonna'], ]], ['stuff' => [ ['id' => 'let'], ]], ['stuff' => [ ['id' => 'you'], ]], ['stuff' => [ ['id' => 'down'], ]], ['stuff' => [ ['id' => 42], ]], ]; $t = microtime(true); for ($i = 0; $i < 10000; $i++) { // original code $ids = []; foreach ($allTheThings as $thing) { $ids = array_merge($ids, array_column($thing['stuff'], 'id')); } $ids = array_unique($ids); } $t = microtime(true) - $t; var_export($ids); echo 'original: '.$t."\n"; $t = microtime(true); for ($i = 0; $i < 10000; $i++) { // improved code $allTheIds = array_map(function ($thing) { return array_column($thing['stuff'], 'id'); }, $allTheThings); $ids = array_unique(array_merge(...$allTheIds)); } $t = microtime(true) - $t; var_export($ids); echo 'improved: '.$t."\n"; $t = microtime(true); for ($i = 0; $i < 10000; $i++) { // new code $ids = array(); foreach ($allTheThings as $thing) { $idstemp = array_column($thing['stuff'], 'id'); foreach ($idstemp as $id) { $ids[$id] = "1"; } } } $t = microtime(true) - $t; var_export($ids); echo 'new: '.$t."\n";

preferences:
36.18 ms | 402 KiB | 5 Q