0

I tried with various data to see the difference between array_slice() and array_splice(), but these two functions resulting the same on my computer.

$input = array("red", "green", "blue", "yellow");
print_r(array_splice($input, 2));  //the result should be {"red", "green"}

$input = array("red", "green", "blue", "yellow");
print_r(array_slice($input, 2));   //the result should be {"blue", "yellow"}

In both cases the following output is displaying: Array ( [0] => blue [1] => yellow )

Any ideas whats going woring?

3
  • 1
    array_splice returns the part you take out. Look into the $input variable instead. Tip: If you're not sure about a function in PHP, read the manual entry: php.net/array_splice Commented Oct 17, 2012 at 10:47
  • 1
    Duplicate of stackoverflow.com/a/5901713/795876 Commented Oct 17, 2012 at 10:49
  • -1 for not looking in the manual and using search. Commented Oct 17, 2012 at 10:51

3 Answers 3

8

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"];
Sign up to request clarification or add additional context in comments.

2 Comments

This answer makes no sense for a non-native speaker – it's hard to tell the difference between slice and chop.
@feeela lol, actually agree; I guess my intention was to provide a short and "clever" answer, but I guess in the end it's not that clever after all. I can update it with more detail to provide more value, although the php docs do a better job fro that
2

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

Comments

0

According to the PHP manual of array_splice:

Returns the array consisting of the extracted elements.

For example:

$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

So the values of $input and of print_r(array_splice... are not the same.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.