array_slice in php

To extract only a subset of the array, use the array_slice() function

$subset = array_slice(array, offset, length);

The array_slice() function returns a new array consisting of a consecutive series of values from the original array. The offset parameter identifies the initial element to copy (0 represents the first element in the array), and the length parameter identifies the number of values to copy. The new array has consecutive numeric keys starting at 0. For example:

$people = array(“Tom”, “Dick”, “Harriet”, “Brenda”, “Jo”);
$middle = array_slice($people, 2, 2); // $middle is array(“Harriet”, “Brenda”)

It is generally only meaningful to use array_slice() on indexed arrays (i.e., those with consecutive integer indices starting at 0)

// this use of array_slice() makes no sense
$person = array('name' => "Fred", 'age' => 35, 'wife' => "Betty");
$subset = array_slice($person, 1, 2); // $subset is array(0 => 35, 1 => "Betty")

Combine array_slice() with list() to extract only some values to variables:

$order = array(“Tom”, “Dick”, “Harriet”, “Brenda”, “Jo”);
list($second, $third) = array_slice($order, 1, 2);
// $second is “Dick”, $third is “Harriet”

Leave a Comment