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

Hàm array_slice() trong PHP

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

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

The array_slice() function extract a slice or portion of an array.

The following table summarizes the technical details of this function.

Return Value: Returns the slice or portion of the array. If the value of the offset parameter is larger than the size of the array, an empty array is returned.
Changelog:

The default value of the length parameter was changed to NULL in PHP 5.2.4. A NULL length now tells the function to use the length of array. Previously, a NULL length was taken to mean a zero length (nothing will be returned).

The optional preserve_keys parameter was added in PHP 5.0.2
Version: PHP 4+

Syntax

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

array_slice(array, offset, length, preserve_keys)

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

<?php
// Sample array
$fruits = array("apple", "banana", "orange", "mango", "papaya", "kiwi");

// Slicing the fruits array 
$result = array_slice($fruits, 1, 3);
print_r($result);
?>

Parameters

The array_slice() 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.
preserve_keys

Optional. Specifies whether to preserve the original index or not.

  • TRUE – The index will be preserved.
  • FALSE – Reindex the sliced elements numerically. This is default.
String keys are always preserved, regardless of this parameter.

More Examples

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

The following example shows what happens if the offset parameter is negative.

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

// Slicing the colors array 
$result = array_slice($colors, -4, 2);
print_r($result);
?>

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

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

// Slicing the colors array 
$result = array_slice($colors, -3, -1);
print_r($result);
?>

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

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

// Slicing the colors array 
$result = array_slice($colors, 2);
print_r($result);
?>

The following example shows what happens if preserve_keys parameter is set to true.

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

// Slicing the colors array 
$result = array_slice($colors, 2, 3, true);
print_r($result);
?>

The following example shows what happens if string keys (i.e. associative array) are used.

<?php
// Sample array
$alphabets = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "d"=>"dog");

// Slicing the alphabets array 
$result = array_slice($alphabets, 1, 2);
print_r($result);
?>

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

Bài viết mới

Advertisements