that actually could work it's just that the array I have to work with comes out 1-based. Believe me I wish I could use something that simple. often php does "simplifications" like resetting the keys which on a low-level I'm sure make sense, but require I write "higher" level coding I'd rather not. Here's what I came up with - and the keys are preserved, but bumped up 1 after the insert:
function array_insert($array, $keyafter, $insert, $options=array()){
/* Created 2010-09-13 by Samuel */
//handle options
extract($options);
if(!$increment)$increment=1;
if(!isset($appendToEnd))$appendToEnd=true;
//handle trivial conditions
if(!is_array($array))return $array;
if(!count($array)){
return array(1=>$insert);
}
//insert the node
foreach($array as $n=>$v){
$a[$n + ($doIncrement ? $increment : 0)]=$v;
if($n==$keyafter){
$doIncrement=$increment;
$a[$n+$increment]=$insert;
}
}
//append to end if $keyafter not reached
if(!$doIncrement && $appendToEnd)$a[$n + $increment]=$v;
return $a;
}
$array=array(
1=>array(2,4,6,8),
2=>array(3,6,9,12),
3=>array(2,3,5,7,11,13,17),
4=>array(log(10), pi(), 8.48191)
);
$insert=array(0,5,10,15,20);
echo '<pre>';
$array=array_insert($array,2,$insert);
print_r($array);