<?php
function array_insert($source, $destination, $offset = NULL, $length = NULL) {
if (!is_array($source) || empty($source)) {
if (is_array($destination) && !empty($destination)) {
return $destination;
}
return array();
}
if (is_null($offset)) {
return array_merge($destination, $source);
}
$offset = var2int($offset);
if (is_null($length)) {
if ($offset === 0) {
return array_merge($source, array_slice($destination, 1));
}
if ($offset === -1) {
return array_merge(array_slice($destination, 0, -1), $source);
}
return array_merge(
array_slice($destination, 0, $offset),
$source,
array_slice($destination, ++$offset)
);
}
if ($offset === 0) {
return array_merge($source, array_slice($destination, $length));
}
$destination_count = count($destination);
$length = var2int($length);
if ($offset > 0) {
if ($destination_count - $offset < 1) {
return array_merge($destination, $source);
}
} else{
if (($t = $destination_count + $offset) < 1) {
return array_merge($source, $destination);
}
$offset = $t;
}
if ($length > 0) {
$length+= $offset;
} elseif ($length < 0 && !($length * -1 < $destination_count)) {
return $source;
} else {
$length = $offset;
}
return array_merge(
array_slice($destination, 0, $offset),
$source,
array_slice($destination, $length)
);
}
?>