Adding/Replacing Elements Inside a PHP Array

In one of my recent posts, I explained how to use array_push, array_pop, array_shift and array_unshift to add and remove elements from the beginning and end of PHP arrays. Today, I’m going to introduce you to the array_splice function, which allows you to insert elements into the middle of the array. Actually, it lets you insert elements at the beginning, end or anywhere in between and even allows you to replace elements that are already in your array.

The function is actually designed to remove specific numbers of elements from an array and optionally replace them with new elements. Let’s look at an example:

$arr1 = array('Item1','Item2','Item3','Item4');
array_splice($arr1,1,1,array('Item2a','Item2b'));

After using the array_splice function, the new array will look like:

Array
(
    [0] => Item1
    [1] => Item2a
    [2] => Item2b
    [3] => Item3
    [4] => Item4
)

Now, let’s look at why the array looks that way. array_splice accepts four parameters. The first two parameters are required; the other two are optional. They are as follows:

  1. Input array [required] – the source array that you want to manipulate with the function
  2. Offset [required] – the numerical offset where you want the replacement to begin. If you use a positive number, the offset will start from the beginning of the source array (for instance, if you wanted to remove the second item in the array, you would use an offset of 1). If you specify a negative offset, the offset will start from the end of the array and work backwards (for instance, if you wanted to remove the last item in the array, you would use an offset of -1).
  3. Length [optional] – The number of elements you want to remove from the array. If you want to remove all of the elements after the offset, you can leave this empty. However, if you are going to specify a replacement, you will need to set the length to something; the PHP manual recommends using count($input) (where $input is the name of your source array).
  4. Replacement [optional] – If you want to insert something into the array in place of the items you removed, you would specify that here. If you only want to insert a single string, you can just declare it as a string. However, if you want to insert multiple elements into the array, you will need to declare the replacement as an array.

In our example above, we wanted to remove the second element of the array and replace it with two new elements. Therefore, we used the command array_splice($arr1,1,1,array('Item2a','Item2b'));. In that example, $arr1 was our input array, we had an offset of 1 (so the removal would begin at the second element in the array), we set a length of 1 (so only one element would be removed) and we used an array of replacements (so multiple items would be inserted into the array).

If you just want to insert new elements into the array, without removing any of the existing items, you can specify a length of 0. When you specify a length of 0, nothing will be removed. Keep in mind, though, that the “replacement” will be inserted at the offset you specified, not after it. Therefore, in our example above, assuming we wanted “Item2a” and “Item2b” to be inserted after “Item2”, we would need to change our offset to 2 instead of 1.

$arr1 = array('Item1','Item2','Item3','Item4');
array_splice($arr1,2,0,array('Item2a','Item2b'));

The code above would produce an array that looks like:

Array
(
    [0] => Item1
    [1] => Item2
    [2] => Item2a
    [3] => Item2b
    [4] => Item3
    [5] => Item4
)

Post Your Comment

Your email address will not be published.