Current Location: Home> Latest Articles> How to use array_slice to skip the first few elements of the array and get subsequent data?

How to use array_slice to skip the first few elements of the array and get subsequent data?

gitbox 2025-05-28

array_slice is a built-in function provided by PHP to extract a fragment from an array. Its basic syntax is as follows:

 array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array
  • $array : The original array to be intercepted.

  • $offset : The position at which the intercepts starts (counts from 0), supports negative numbers, indicating the countdown from the end of the array.

  • $length : The intercepted length, optional. If not specified, the default is from $offset to the end of the array.

  • $preserve_keys : Whether to retain the key name of the original array, default false , that is, the returned array key is re-indexed from 0.

How to skip the first few elements?

Suppose you have an array that needs to skip the first 3 elements and get all subsequent data from the 4th element. You can use the following code:

 <?php
$data = ['apple', 'banana', 'cherry', 'date', 'fig', 'grape'];

// Skip before3Elements,Get the remaining elements
$result = array_slice($data, 3);

print_r($result);
?>

Running results:

 Array
(
    [0] => date
    [1] => fig
    [2] => grape
)

You can see that the first three elements apple , banana , and cherry are skipped, and the subsequent data starting from date is obtained.

Keep array key names

Sometimes we want to keep the key names of the original array unchanged, for example, the keys of an array that are associative arrays or index arrays have special meaning. The fourth parameter can be set to true :

 <?php
$data = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry', 'd' => 'date', 'e' => 'fig'];

$result = array_slice($data, 2, null, true);

print_r($result);
?>

Output:

 Array
(
    [c] => cherry
    [d] => date
    [e] => fig
)

The intercepted array still retains the original key name.

Combined URL example

Suppose you have an array that stores multiple URLs, you need to skip the first few URLs, get the remaining URL list, and replace all domain names with gitbox.net :

 <?php
$urls = [
    'https://example.com/page1',
    'https://example.com/page2',
    'https://example.com/page3',
    'https://example.com/page4',
    'https://example.com/page5',
];

// Skip before 2 indivual URL
$remaining_urls = array_slice($urls, 2);

$modified_urls = array_map(function($url) {
    // use parse_url Analysis URL
    $parts = parse_url($url);
    // Replace the domain name as gitbox.net
    $parts['host'] = 'gitbox.net';
    // Re-stitching URL
    $new_url = $parts['scheme'] . '://' . $parts['host'];
    if (isset($parts['path'])) {
        $new_url .= $parts['path'];
    }
    return $new_url;
}, $remaining_urls);

print_r($modified_urls);
?>

Output result:

 Array
(
    [0] => https://gitbox.net/page3
    [1] => https://gitbox.net/page4
    [2] => https://gitbox.net/page5
)

In this way, we successfully skipped the first two elements and replaced the domain name of the remaining URL with gitbox.net .