• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
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.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 2.  Check if Variable Exists in php with isset() Function
  • 3.  echo php – Output One or More Strings
  • 4.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 5.  php max – Find Maximum Value of Array in php
  • 6.  Remove Duplicates from Array in php with array_unique()
  • 7.  php array_walk() – Modify Elements in Array with Callback Function
  • 8.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 9.  Get Current Location Latitude Longitude in php
  • 10.  php e – Using M_E and exp() Function to Get Euler’s Constant e

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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