Javatpoint Logo
Javatpoint Logo

PHP array_slice() function

The array_slice() function is an inbuilt function of PHP. The array_slice() function is used to extract a slice of an array. This function was introduced in 4.0.

Syntax

Parameters

Parameter Description Is compulsory
array Specifies an array. compulsory
starting Specifies the beginning position of the slice in the array. compulsory
Length Length of the slice. Optional
Preserve keys Specify TRUE or FALSE, whether the function shall preserve the array's keys or not. The default value is FALSE. Optional

Return

The array_slice( ) function returns the selected parts of an array. If the offset is larger than the size of the array, this function will return an empty array.

Example 1

Output:

Array
(
    [0] =>sehwag
    [1] =>rahul
)

Example 2

Output:

Array
(
    [0] => Hyundai
    [1] => Toyota
)

Example 3

Output:

Array
(
    [2] => Hyundai
    [3] => Toyota
)

Example 4

Output:

Array
(
    [0] => DBMS
)
Array
(
    [0] =>os
)
Array
(
    [0] => java
    [1] => C++
    [2] =>os
    [3] => DBMS
)





Help Others, Please Share

facebook twitter pinterest