Thursday 5 June 2014

Why we use array_slice.

array_slice - ( ) Cut a specified slice of elements from an array-

Use of array_slice is basically returns the selected part of an array.The array_slice() array function in PHP will cut a specified slice of elements from an array. It can take up to four parameters, but only requires two parameters to operate at a basic level. Parameter one is the array you wish to manipulate. Parameter two is the offset(where you want the array to begin to be sliced from). Parameter three is the length into the array you wish to slice. Parameter four is set to "true" to preserve keys, or "false" to not preserve keys.

Syntax-

array_slice(array,start,length,preserve)

For Example-

<html>
<body>

<?php
                  $a=array("Html","Css","Php","Java","C++");
                   print_r(array_slice($a,2));
?>

</body>
</html>

Output-
Array ( [0] => Php[1] => Java[2] => C++)


Posted by Amit Tiwari (Facebook)

No comments:

Post a Comment