Add elements to an array before or after a specific index or key:
<?php
function array_push_before($src,$in,$pos){
if(is_int($pos)) $R=array_merge(array_slice($src,0,$pos), $in, array_slice($src,$pos));
else{
foreach($src as $k=>$v){
if($k==$pos)$R=array_merge($R,$in);
$R[$k]=$v;
}
}return $R;
}
function array_push_after($src,$in,$pos){
if(is_int($pos)) $R=array_merge(array_slice($src,0,$pos+1), $in, array_slice($src,$pos+1));
else{
foreach($src as $k=>$v){
$R[$k]=$v;
if($k==$pos)$R=array_merge($R,$in);
}
}return $R;
}
$src=array("A","B","C");
$in=array("X","Y");
var_dump(array_push_before($src,$in,1));
var_dump(array_push_after($src,$in,1));
$src=array('a'=>"A",'b'=>"B",'c'=>"C");
$in=array('x'=>"X",'y'=>"Y");
var_dump(array_push_before($src,$in,1));
var_dump(array_push_before($src,$in,'b'));
var_dump(array_push_after($src,$in,1));
var_dump(array_push_after($src,$in,'b'));
?>