Array_splice with 3 arguments

Hi,

I have a problem with understanding array_splice(…) operation in the question at the bottom. -1 is used for length or offset but how we use this value in the context of array because array indexes and size is positive. If we suppose -1 as the end of array, then it means we have to remove elements from index 1 to the end of array and this would leave only “red” as the answer which is wrong.

$input = array(“red”, “green”, “blue”, “yellow”);
array_splice($input, 1, -1);

Somebody please guide me.

Zulfi.

The examples in Free Online PHP Manual may be useful:

https://www.php.net/manual/en/function.array-splice.php

1 Like

What result are you expecting?

1 Like

Hi,
Thanks for your response. The link says:

If offset is negative then the start of the removed portion is at that offset from the end of the array array.

So we will start from end and go to index 1 from the end which is blue then we would remove “blue”.

so array would be (“red”, “green”, “yellow”). The answer is not right. Please guide me.
Zulfi.

It’s not the offset you’ve set negative.

Danger Will Robinson, Danger!

Array indexes are zero indexed. Zero is neither positive nor negative.

1 Like

Understand you are not using or talking about array keys but rather a number from a direction. Add an extra color to the middle of your example and you will see the result is the same.

$input = array("red", "green", "blue", "Orange", "yellow");

This is because you are saying “I will keep the first one”

array_splice($input, 1);

… and I will keep the last one because you have a minus sign (end) so from the (end) 1 place will be kept. Everything between is removed.

array_splice($input, 1, -1);

If you had -2 (from the end) then the two end values are kept. e.g.

array_splice($input, 1, -2);
Array
(
    [0] => red
    [1] => Orange
    [2] => yellow
)

Say you don’t want the first item but the last three.

array_splice($input, 0, -3);
Array
(
    [0] => blue
    [1] => Orange
    [2] => yellow
)

OR again, just the first three items.

array_splice($input, 3);
Array
(
    [0] => red
    [1] => green
    [2] => blue
)

I more example to really confuse you. Let’s say you want to remove the third item and replace it with two more items but the rest you’ll keep. So “start position” or the number you want to keep is 2 and you want to remove 1 item from the “start position” and “replace” it with two items written as an array.

$input = array("red", "green", "blue", "Orange", "yellow");
$replace = array("lavender","black"); 

array_splice($input, 2, 1,$replace);

The result.

Array
(
    [0] => red
    [1] => green
    [2] => lavender
    [3] => black
    [4] => Orange
    [5] => yellow
)

…and if you were skipping 2 and removing 2 and replacing those with your $replace array.

array_splice($input, 2, 2,$replace);
Array
(
    [0] => red
    [1] => green
    [2] => lavender
    [3] => black
    [4] => yellow
)
4 Likes

Hi Drummin,
I understood all your php commands but the text has some confusion because you are using the word keep which looks that you don’t want to delete the 2nd position but actually you are deleting:

So “start position” or the number you want to keep is 2 and you want to remove 1 item from the “start position” and “replace” it with two items written as an array.

It might be:
So “start position” or the index from which you want to do the processing is 2…

.
But I understood the command i.e. thanks to the previous post related to the 4 argument of array_splice(…), but your work helped a lot.

God blesses you all.

Zulfi.

In using the work KEEP I just was trying to speak in terms that might make it more clear that those 2 items will be kept, whereas using the term Start Position does not really explain if an item is “From” or “Between” i.e. it 2 is kept or not… One could argue we often specify to remove something from two points, e.g. 10 to 20.

Looking at my example, we are keeping the first 2 items Red and Green and not deleting the second one.

Array
(
    [0] => red
    [1] => green
etc.
1 Like

Hi,

Thanks.
God blesses you.

Zulfi.