How to use array slice in PHP
In this article, I will explain how the array_slice() function can be used in PHP.
array_slice() function in PHP
-
The array_slice() function returns selected parts of an array.
-
array_slice() function returns the sequence of elements from the array as specified by the offset and length parameters.
-
In array_slice() function, the sequence will start at that offset in the array, If offset is positive .
-
In array_slice() function, the sequence will start that far from the end of the array, If offset is negative.
Syntax
array_slice(array,start,length,preserve) |
Parameter
-
array array is required parameter. Specifies an array.
-
start start is required parameter. start is the numeric value. Specifies where the function will start the slice.
-
length length is Optional parameter. length is the numeric value. Specifies the length of the returned array.
-
preserve preserve is Optional parameter. Default value of preserve is false. Possible value
Example
The following example show to how the array_slice() function can be used in PHP.
<html>
<body>
<h3 style="color: darkslategrey;">array_slice() function example in PHP</h3>
<?php
$list = array('Student','Engg.','Teacher','Worker');
$result= array_slice($list,3);
print_r($result);
echo "<br>";
$result= array_slice($list,-2,1);
print_r($result);
echo "</br>";
$result= array_slice($list,0,4);
print_r($result);
?>
</body>
</html>
|
Output
You may also want to read these related articles here
Ask Your Question
Got a programming related question? You may want to post your question here