PHP

PHP Menu

PHP

array_splice() Function - Definition, Syntax, Parameters, Examples

Definition

The array_splice() function removes a portion or slice of an array and replaces it with the elements of another array. If no replacement array is specified, this function simply removes the elements.

Syntax

array_splice(array, start, length, array)

Parameters

Parameter Description
array Required. Specifies an array.
start Required. Numeric value. Specifies where the function will start removing elements. 0 = the first element. If this value is set to a negative number, the function will start that far from the last element. -2 means start at the second last element of the array.
length Optional. Numeric value. Specifies how many elements will be removed, and also length of the returned array. If this value is set to a negative number, the function will stop that far from the last element. If this value is not set, the function will remove all elements, starting from the position set by the start-parameter.
array Optional. Specifies an array with the elements that will be inserted to the original array. If it's only one element, it can be a string, and does not have to be an array.

Example

<?php
// Example 1 $ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28); $ages2 = array("Mark" => 23, "Jeff" => 21); array_splice($ages, 0, 2, $ages2); print_r($ages); echo "<br>";
// Example 2 $ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28); $ages2 = array("Mark" => 23, "Jeff" => 21); print_r(array_splice($ages, 1, 0, $ages2)); echo "<br>"; print_r($ages); echo "<br>";

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods