<?php
function inject_array( &$array, array $var, $pos = null )
{
$tmp_storage = array();
$pos = (int) $pos;
if ( $pos > count( $array ) )
{
foreach ( $var as $key => $value )
{
$array[] = $value;
}
return;
}
if ( array_key_exists( $pos, $array ) )
{
$tmp_storage = array_slice( $array, $pos );
}
$injection_position = $pos;
foreach ( $var as $key => $value )
{
$array[ $injection_position ] = $value;
++$injection_position;
}
$pos = $pos + count( $var );
$new_length = $pos + count( $tmp_storage );
for ( $i = $pos, $j = 0; $i < $new_length; $i++, $j++ )
{
$array[ $i ] = $tmp_storage[ $j ];
}
}
$sample = array(
'test',
'test1',
'test2',
);
inject_array( $sample, array( 'test4', 'test5' ), 1 );
var_dump( $sample ); exit;