Hướng dẫn
Quảng cáo

Hàm array_splice() trong PHP

Hướng dẫn cách sử dụng hàm array_splice() về mảng trong lập trình PHP

Tác dụng của hàm array_splice()

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.

The following table summarizes the technical details of this function.

Return Value: Returns an array consisting of the extracted elements.
Version: PHP 4+

Tip: The array_splice() function selects a sequence of elements using the same rules as array_slice(). But, unlike array_slice() the array_splice() function modifies the original array by removing or adding the elements instead of simply returning the slice of the array.


Syntax

The basic syntax of the array_splice() function is given with:

array_splice(array, offset, length, replacement)

The following example shows the array_splice() function in action.

<?php
// Sample arrays
$input = array("apple", "banana", "orange", "mango", "papaya", "kiwi");
$replacement = array("lemon", "carrot", "corn");

// Performing array splice
$result = array_splice($input, 2, 3, $replacement);
print_r($result);
print_r($input);
?>

The original keys in the replacement array are not preserved when spliced into the input array. They are replaced with numeric keys. Let's try out an example and see how it works:

<?php
// Sample arrays
$input = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "d"=>"dog");
$replacement = array("x"=>"xylophone", "y"=>"yacht");

// Performing array splice
$result = array_splice($input, 1, 2, $replacement);
print_r($result);
print_r($input);
?>

Parameters

The array_splice() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to work on.
offset

Required. Specifies the starting point of the slice (0 represents the first element).

If you specify a negative number, the starting point will be that many elements before the last element, e.g. -2 means starting point will be second last element of the array.
length

Optional. Specifies the number of elements to extract.

If its value is set to a negative number then the slicing will stop that many elements from the end of the array. If the length parameter is not specified, all the elements from the starting point to the end of the array will be returned.
replacement Optional. Specifies the replacement array. If it is specified, then the removed elements are replaced with the elements from this array.

Tip: If replacement is just one element it is not necessary to put array() or square brackets around it, as PHP will automatically typecast it to array (i.e. (array) $replacement).

Note: If the value of offset and length parameters are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset.


More Examples

Here're some more examples showing how array_splice() function basically works:

The following example shows what happens if no replacement array is specified.

<?php
// Sample array
$input = array("red", "green", "blue", "pink", "yellow", "black");

// Performing array splice
$result = array_splice($input, 3, 2);
print_r($result);
print_r($input);
?>

The following example shows what happens if both offset and length parameters are negative.

<?php
// Sample array
$input = array("red", "green", "blue", "pink", "yellow", "black");

// Performing array splice
$result = array_splice($input, -4, -1);
print_r($result);
print_r($input);
?>

The following example shows what happens if length parameter is omitted or not specified.

<?php
// Sample array
$input = array("red", "green", "blue", "pink", "yellow", "black");

// Performing array splice
$result = array_splice($input, 2);
print_r($result);
print_r($input);
?>

The following example shows what happens if length parameter value is set to 0.

<?php
// Sample arrays
$input = array("red", "green", "blue", "pink", "yellow", "black");
$replacement = array("purple", "maroon", "violet");

// Performing array splice
$result = array_splice($input, 2, 0, $replacement);
print_r($result);
print_r($input);
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements