AppDividend
Latest Code Tutorials

PHP Array Slice Example | array_slice() Function Tutorial

0

PHP Array Slice Example | array_slice() Function Tutorial is today’s topic. The array_slice() function returns selected parts of an array. PHP array_slice() function is used to extract a slice of an array. PHP array_slice() is an inbuilt function of PHP and is used to fetch a part of an array by slicing through it, based on the user’s preference.

PHP Array Slice Example

The syntax of array_slice() function is following.

array_slice(array,start,length,preserve)

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 a numeric value as well. 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.

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);

The output is following.

PHP Array Slice Example

 

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

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);

The output is following.

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 set to false

Let’s pass the fourth parameter which is set 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 due 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);

The output is following.

The preserve parameter set to true

 

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

Conclusively, PHP Array Slice Example | array_slice() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

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