So you have an array
$input = ["red", "green", "blue", "yellow"];
array_slice
Running array_slice($input, 2) would return you the part that you're requesting by the $offset (2) and $length - the 3d parameter that you've omitted (which would mean as many as there's left). Also the interesting thing here is that the $input is getting passed not by reference, meaning that it's going to be left unchanged.
$result = array_slice($input, 2);
// $input == [0 => "red", 1 => "green", 2 => "blue", 3 => "yellow"];
// $result == [0 => "blue", 1 => "yellow"];
There's an optional 4th parameter to preserve the keys, which would mean the returned keys are unchanged.
$result = array_slice($input, 2, null, true);
// $result == [2 => "blue", 3 => "yellow"];
array_splice
This function is similar to array_slice, except this time the array is passed by reference. So the function can change the initial array now. Additionally the 4th parameter is accepting an array that should replace the sliced part (if omitted it just means that that part is replaced with an empty array).
$result = array_splice($input, 2);
// $input = [0 => "red", 1 => "green"];
// $result == [0 => "blue", 1 => "yellow"];
$result = array_splice($input, 2, null, ["brown", "black"]);
// $input = [0 => "red", 1 => "green", 2 => "brown", 3 => "black"];
// $result == [0 => "blue", 1 => "yellow"];
Answer from Slayer Birden on Stack OverflowSo you have an array
$input = ["red", "green", "blue", "yellow"];
array_slice
Running array_slice($input, 2) would return you the part that you're requesting by the $offset (2) and $length - the 3d parameter that you've omitted (which would mean as many as there's left). Also the interesting thing here is that the $input is getting passed not by reference, meaning that it's going to be left unchanged.
$result = array_slice($input, 2);
// $input == [0 => "red", 1 => "green", 2 => "blue", 3 => "yellow"];
// $result == [0 => "blue", 1 => "yellow"];
There's an optional 4th parameter to preserve the keys, which would mean the returned keys are unchanged.
$result = array_slice($input, 2, null, true);
// $result == [2 => "blue", 3 => "yellow"];
array_splice
This function is similar to array_slice, except this time the array is passed by reference. So the function can change the initial array now. Additionally the 4th parameter is accepting an array that should replace the sliced part (if omitted it just means that that part is replaced with an empty array).
$result = array_splice($input, 2);
// $input = [0 => "red", 1 => "green"];
// $result == [0 => "blue", 1 => "yellow"];
$result = array_splice($input, 2, null, ["brown", "black"]);
// $input = [0 => "red", 1 => "green", 2 => "brown", 3 => "black"];
// $result == [0 => "blue", 1 => "yellow"];
array_splice($input, 2) removes the elements at offset 2, replacing them with nothing (you're not specifying anything to replace) and returns an array consisting of the extracted elements: that's blue and yellow. The transmogrified original array ($input) is modified by reference, not returned
array_slice($input, 2) returns all elements from offset 2 in $input: that's blue and yellow
I think you need to do that manually:
# Insert at offset 2
$offset = 2;
$newArray = array_slice($oldArray, 0, $offset, true) +
array('texture' => 'bumpy') +
array_slice($oldArray, $offset, NULL, true);
Based on soulmerge's answer I created this handy function:
function array_insert($array,$values,$offset) {
return array_slice($array, 0, $offset, true) + $values + array_slice($array, $offset, NULL, true);
}