What does array splice do in PHP - onlyxcodes

Thursday 4 July 2024

What does array splice do in PHP

Welcome PHP Coder I will demonstrate the functions of array splice in PHP in this tutorial.


Many built-in PHP functions are available to assist you in editing and working with array data while working with arrays. The array_splice() function is one such effective function. This tutorial aims to provide an in-depth understanding of the array_splice() function, including its syntax and implementation through a series of examples.


what does array splice do in php

Table Content

1. What is the `array_splice()` Function?

2. Return Values

3. Remove elements from an array:

4. Replace elements in an array:

5. Insert elements into an array:


1. What is the `array_splice()` Function?

A PHP method called array_splice() can take out a specific section of an array and replace it with a different set of components. This function is useful when you want to change an array without writing complicated code by adding or removing elements. An input array, a length, a starting index, and an optional array of replacement items are required for the array_splice() function to function. The deleted entries are then returned as a new array.


Here is the basic syntax of array_splice():


array_splice(array &$array, int $offset [, int $length = count($array) [, mixed $replacement = array()]]): array

Parameters


array: The input array.


offset: The index starting at which items will be eliminated. The start will be counted from the end of the array if it is negative.


length:  The quantity of components to be eliminated (optional). All elements from the offset to the end of the array will be eliminated if omitted. If it is zero, nothing will be eliminated.


replacement: (Optional) A list of elements to take the place of the ones that were eliminated. No elements will be added if they are left off.


2. Return Values

The function returns an array of the removed elements.


In this instance, we began by removing two components from index 1. The elements that were eliminated are returned in a new array after the input array has been altered.


<?php

$input = [1, 2, 3, 4, 5];
$offset = 1;
$length = 2;

$removed = array_splice($input, $offset, $length);

print_r($input); // Output: Array ( [0] => 1 [1] => 4 [2] => 5 )
print_r($removed); // Output: Array ( [0] => 2 [1] => 3 )*/

?>

3. Remove elements from an array:

Two elements are taken out of the array's end in this instance. The elements that were eliminated are returned in a new array after the input array has been altered.


<?php

$array = [1, 2, 3, 4, 5];
$removed = array_splice($array, 2, 2, ['a', 'b']);

print_r($array); // Outputs: Array ( [0] => 1 [1] => 2 [2] => a [3] => b [4] => 5 )
print_r($removed); // Outputs: Array ( [0] => 3 [1] => 4 )

?>

4. Replace elements in an array:

In this example, we used the entries from the replacement array to replace two elements, starting at index 1. The elements that were eliminated are returned in a new array after the input array has been altered.


<?php

$array = [1, 2, 3, 4, 5];
$removed = array_splice($array, 2, 2, ['a', 'b']);

print_r($array); // Outputs: Array ( [0] => 1 [1] => 2 [2] => a [3] => b [4] => 5 )
print_r($removed); // Outputs: Array ( [0] => 3 [1] => 4 )

?>


5. Insert elements into an array:

We add two elements to the array in this example. The elements that were eliminated are returned in a new array after the input array has been altered.


<?php

$array = [1, 2, 3, 4, 5];
$removed = array_splice($array, 2, 0, ['a', 'b']);

print_r($array); // Outputs: Array ( [0] => 1 [1] => 2 [2] => a [3] => b [4] => 3 [5] => 4 [6] => 5 )
print_r($removed); // Outputs: Array ( )

?>

No comments:

Post a Comment

Post Bottom Ad