PHP Array Slice: How to Use array_slice() Function

0
481
PHP Array Slice Example | array_slice() Function Tutorial

To extract a slice from an array in PHP, use the array_slice() function. The array_slice() function can be used to remove elements from an array, but it’s simple to use a custom function. So first, let’s see how to use the array_slice() function.

PHP array_slice

PHP array_slice() is a built-in function that returns selected parts of an array. The array_slice() function extracts a slice of an array. The array_slice() method starts the slice from the third array element and returns the rest of the elements in the array.

Syntax

The syntax of array_slice() function is following.

array_slice(array,start,length,preserve)

Arguments

An array parameter is an input array. It is a required parameter.

The start is a required parameter, and it is a numeric value. Specifies where the function will start the slice: 0th index = the first element.

The length is an optional parameter, and it is also a numeric value. If this value is set to a negative number, the function will start slicing that far from the last element.

The preserve parameter is optional and Specifies if the function should preserve or reset the keys. Possible values.

true –  Preserve keys.

false – Default. Reset keys.

Example

See the below example of PHP array_slice() function.

<?php

// app.php

$data = ['a' => 'krunal', 
        'b' => 'ankit',
        'f' => 'nehal', 
        'k' => 'krunal'];
$extractedValue = array_slice($data, 1, 2);
print_r($extractedValue);

See the output.

PHP Array Slice Example

So, we have passed the array, extracting the array from starting index 1 to the length of 2. That means and are the extracted array output.

The array_slice() method returns an array with the current item. You must use array_slice($array, $index+1) to get the next items.

Using a negative start parameter

Let’s use the negative start parameter and see the output.

<?php

// app.php

$data = ['a' => 'krunal', 
        'b' => 'ankit',
        'f' => 'nehal', 
        'k' => 'krunal'];
$extractedValue = array_slice($data, -2, 2);
print_r($extractedValue);

See the output.

Using a negative start parameter

So, we have passed the -2 parameter, which means it starts from the end of an array. So, from the last, the index is 2, and then the length is 2. That means it will return an array containing the last two elements from the array.

The preserve parameter is set to false.

Let’s pass the fourth parameter, set it to false, and see the output. That means it does not preserve the keys and starts with the 0th index. See the below code.

<?php

// app.php

$data = ['krunal', 
        'ankit',
        'nehal', 
        'dhaval'];
$extractedValue = array_slice($data, -2, 2, false);
print_r($extractedValue);

See the below output.

The preserve parameter set to false

We are fetching the array filled with the last two elements; still, the indexes are starting from 0 to set the preserve parameter to false.

Let’s pass the true value to the preserve argument and see the output.

<?php

// app.php

$data = ['krunal', 
        'ankit',
        'nehal', 
        'dhaval'];
$extractedValue = array_slice($data, -2, 2, true);
print_r($extractedValue);

See the output.

The preserve parameter set to true

So, if set the preserve key to true, then it keeps the index as in the original array.

That’s it for PHP array_slice() function.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.