3v4l.org

run code in 500+ PHP versions simultaneously
<?php $a = [ [ 'position' => 0, 'color' => 'violet' ], [ 'position' => 2, 'color' => 'red' ], [ 'position' => 5, 'color' => 'green' ], ]; $b = [ [ 'color' => 'yellow' ], [ 'color' => 'orange' ], [ 'color' => 'pink' ], [ 'color' => 'blue' ], ]; $pos = array_column($a, "color", "position"); $count = count($pos) + count($b); $j = 0; for($i=0; $i<$count-1; $i++){ if(!isset($pos[$i])){ $pos[$i] = $b[$j]; $j++; }else{ $pos[$i] = [$pos[$i]]; } } ksort($pos); var_dump($pos);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.32, 8.3.0 - 8.3.32, 8.4.1 - 8.4.23, 8.5.0 - 8.5.8
array(6) { [0]=> array(1) { [0]=> string(6) "violet" } [1]=> array(1) { ["color"]=> string(6) "yellow" } [2]=> array(1) { [0]=> string(3) "red" } [3]=> array(1) { ["color"]=> string(6) "orange" } [4]=> array(1) { ["color"]=> string(4) "pink" } [5]=> array(1) { [0]=> string(5) "green" } }

preferences:
91.51 ms | 1624 KiB | 4 Q