3v4l.org

run code in 300+ PHP versions simultaneously
<?php $first = [1, 2, 3, 4]; $second = [10, 20, 30, 40]; $third = []; $fourth = ['a', 'b']; $pool = [$first, $second, $third, $fourth]; $result = []; while (count($pool) > 1) { $pullFrom = array_rand($pool); if (!$pool[$pullFrom]) { unset($pool[$pullFrom]); continue; } $result[] = array_shift($pool[$pullFrom]); } var_export(array_merge($result, ...$pool));

preferences:
23.09 ms | 407 KiB | 5 Q