PHP array_slice() Function
Topic: PHP Array ReferencePrev|Next
Description
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 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:
The following example shows the array_slice()
function in action.
Example
Run this code »<?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.
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.
Example
Run this code »<?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.
Example
Run this code »<?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.
Example
Run this code »<?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
.
Example
Run this code »<?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.
Example
Run this code »<?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);
?>