Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
$arr = [1=>"A", "2" => "B", "6" => "Z"];
$un = ["G", "H", "I"];
//$out = [1=>"A", "2" => "B", 3 => "G", 4 => "H", 5 => "I" "6" => "Z"];
$order = 0;
foreach($arr as $key=>$val) {
while ((int)$key-$order > 1 && count($un)>0) {
printf("order: %d, key, %d" . PHP_EOL, $order, $key );
$order++;
$arr[$order] = array_shift($un);
}
$order++;
}
print_r($arr);