Using PHP array_slice() Function: Basics, Syntax, and Practical Tips

This article explains the PHP array_slice() function, covering its basic syntax, practical examples for extracting sub‑arrays, using negative offsets, selecting specific keys with array_flip and array_intersect_key, and removing elements via array_splice, providing clear code snippets and output demonstrations.

php Courses
php Courses
php Courses
Using PHP array_slice() Function: Basics, Syntax, and Practical Tips

Basic Usage of array_slice()

The array_slice() function returns a portion of an array, creating a new array that contains a specified number of elements starting from a given offset.

Function Signature

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] )

Parameters: $array – the source array. $offset – the starting position. $length – the number of elements to extract (optional). $preserve_keys – whether to preserve the original keys.

Simple Example

$arr = array('a', 'b', 'c', 'd', 'e');
$part = array_slice($arr, 2, 2);
print_r($part);

Output:

Array
(
    [0] => c
    [1] => d
)

This extracts two elements starting from the third element, producing a new array $part containing 'c' and 'd'.

Practical Tips

Extracting from the End

Set $offset to a negative number to count from the end of the array.

$arr = array('a', 'b', 'c', 'd', 'e');
$part = array_slice($arr, -3);
print_r($part);

Output:

Array
(
    [2] => c
    [3] => d
    [4] => e
)

Selecting Specific Keys

Combine array_flip() and array_intersect_key() to extract elements with particular keys.

$arr = array('a', 'b', 'c', 'd', 'e');
$keys = array(2, 4);
$flipped = array_flip($arr);
$selected = array_intersect_key($flipped, array_flip($keys));
$part = array_intersect_key($arr, $selected);
print_r($part);

Output:

Array
(
    [2] => c
    [4] => e
)

Removing Elements

Use array_splice() to delete a range of elements from the original array.

$arr = array('a', 'b', 'c', 'd', 'e');
$part = array_splice($arr, 2, 2);
print_r($arr);

Output:

Array
(
    [0] => a
    [1] => b
    [4] => e
)

The call removes the third and fourth elements, leaving the original array with the remaining items.

Conclusion

The array_slice() function is a versatile tool for handling arrays in PHP. Mastering its syntax and related techniques such as negative offsets, key selection, and element removal can greatly improve the efficiency and readability of your PHP code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPCode Examplesarray manipulationarray_slice
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.