PHP - Function array_slice()


Advertisements


Syntax

array_slice($array, $offset [,$length [,$preserve_keys]] );

Definition and Usage

The function returns the sequence of elements from the array array as specified by the offset and length parameters.

Parameters

Sr.No Parameter & Description
1

array(Required)

It specifies an array.

2

offset(Required)

It specifies where the function will start the slice.

3

length(Optional)

It specifies the length of the slice.

4

preserve_keys(Optional)

It gives TRUE to preserve keys and FALSE to reset keys. Default is FALSE.

Return Values

It returns the sequence of elements.

Example

Try out following example −

Live Demo
<?php
   $input = array("a", "b", "c", "d", "e");
   
   print_r(array_slice($input, 2, -1));
   print_r(array_slice($input, 2, -1, true));
?> 

This will produce the following result −

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

php_function_reference.htm

Advertisements