3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arrayA = [ ["sentence" => "Hello world", "nextSpeaker" => 0], ["sentence" => "Hello world again", "nextSpeaker" => 1], ["sentence" => "Hello world twice!", "nextSpeaker" => 1], ]; $arrayB = [ ["sentence" => "Bye world", "nextSpeaker" => 1], ["sentence" => "Bye world again", "nextSpeaker" => 0], ["sentence" => "Bye world twice", "nextSpeaker" => 0], ]; $arrays = [$arrayA, $arrayB]; $pullFrom = 0; $result = []; while ($arrays[0] && $arrays[1]) { $result[] = $row = array_shift($arrays[$pullFrom]); $pullFrom = $row['nextSpeaker'] ? 1 - $pullFrom : $pullFrom; } var_export(array_merge($result, ...$arrays));
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.7
array ( 0 => array ( 'sentence' => 'Hello world', 'nextSpeaker' => 0, ), 1 => array ( 'sentence' => 'Hello world again', 'nextSpeaker' => 1, ), 2 => array ( 'sentence' => 'Bye world', 'nextSpeaker' => 1, ), 3 => array ( 'sentence' => 'Hello world twice!', 'nextSpeaker' => 1, ), 4 => array ( 'sentence' => 'Bye world again', 'nextSpeaker' => 0, ), 5 => array ( 'sentence' => 'Bye world twice', 'nextSpeaker' => 0, ), )

preferences:
111.97 ms | 1361 KiB | 4 Q