• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / PHP / php array_splice() – Remove, Insert, and Replace Elements in Array

php array_splice() – Remove, Insert, and Replace Elements in Array

April 13, 2022 Leave a Comment

The php array_splice() function gives us the ability to remove elements from an array by their position, insert new elements at a specific position, and replace elements.

To remove an element from an array, pass an array and the position of the element you want to remove to array_splice()

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Remove element at position 1
array_splice($array, 1);
print_r($array);

// Output: 
Array
(
    [0] => elephant
    [1] => rabbit
    [2] => salmon
    [3] => alligator
    [4] => whale
)

To replace elements in an array with array_splice(), pass an array, the starting position of where you will replace, the number of elements you want to remove, and then the elements you want to insert.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Replace element at position 2
array_splice($array, 2, 1, "monkey");
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => monkey
    [3] => salmon
    [4] => alligator
    [5] => whale
)

To insert elements in an array with array_splice(), pass an array, the starting position of where you will insert, 0 to make 0 deletions, and then the elements you want to insert.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Insert element at position 3
array_splice($array, 3, 0, "monkey");
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => rabbit
    [3] => monkey
    [4] => salmon
    [5] => alligator
    [6] => whale
)

When working with collections of data in php, the ability to add to, remove from, and manipulate those collections is valuable.

The php array_splice() function allows us to do all of this. With array_splice(), you can remove elements based on their position, insert new elements at a specific position, and also make replacements.

array_splice() takes two required parameter and two optional parameters. The required parameters are the array you want to operate on and the starting position of the portion you want to remove.

The optional parameters are the length of the portion you want to remove and the elements you want to insert.

So, if you want to remove an element from an array, pass an array and the position of the element you want to remove to array_splice().

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Remove element at position 1
array_splice($array, 1);
print_r($array);

// Output: 
Array
(
    [0] => elephant
    [1] => rabbit
    [2] => salmon
    [3] => alligator
    [4] => whale
)

If you want to remove multiple elements, also pass the number of elements you want to remove.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Remove element at position 1
array_splice($array, 1, 2);
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => salmon
    [2] => alligator
    [3] => whale
)

Using array_splice() to Replace Elements in Array in php

You can make replacements in arrays with array_splice().

To replace elements in an array with array_splice(), pass an array, the starting position of where you will replace, the number of elements you want to remove, and then the elements you want to insert.

Below is an example of how to replace an element in an array with array_splice() in php.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Replace element at position 2
array_splice($array, 2, 1, "monkey");
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => monkey
    [3] => salmon
    [4] => alligator
    [5] => whale
)

If you want to replace more than one element, pass an array to array_splice().

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Replace two elements at position 2
array_splice($array, 2, 2, array("monkey","giraffe"));
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => monkey
    [3] => giraffe
    [4] => alligator
    [5] => whale
)

Using array_splice() to Insert Elements in Array in php

You can make insertions in an array with array_splice().

To insert elements in an array with array_splice(), pass an array, the starting position of where you will insert, 0 to make 0 deletions, and then the elements you want to insert.

Below is an example of how to insert elements into an array using array_splice() in php.

$array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale');

// Insert element at position 3
array_splice($array, 3, 0, "monkey");
print_r($array);

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => rabbit
    [3] => monkey
    [4] => salmon
    [5] => alligator
    [6] => whale
)

Hopefully this article has been useful for you to learn how to use array_splice() to remove, insert and replace elements in arrays in your php programs.

Other Articles You'll Also Like:

  • 1.  Remove String from String in php with str_replace()
  • 2.  php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function
  • 3.  php array_slice() – Get Slice of Elements from Array
  • 4.  php acos – Find Arccosine and Inverse Cosine of Number
  • 5.  How to Check If String Contains Substring in PHP
  • 6.  php Move File from One Directory to Another Folder
  • 7.  php is_array() Function – Check if Variable is Array in php
  • 8.  php rtrim() Function – Remove Whitespace at End of String
  • 9.  Get Days in Month Using php
  • 10.  PHP Numerically Indexed Array Begin With Position 0

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy