PHP array_slice() Function Last Updated : 20 Jun, 2023 Comments Improve Suggest changes Like Article Like Report The array_slice() is an inbuilt function of PHP and is used to fetch a part of an array by slicing through it, according to the users choice.Syntax: array_slice($array, $start_point, $slicing_range, preserve) Parameters: This function can take four parameters and are described below: $array (mandatory): This parameter refers to the original array, we want to slice.$start_point (mandatory): This parameter refers to the starting position of the array from where the slicing need to be performed. It is mandatory to supply this value. If the value supplied is negative, then the function starts slicing from the end of the array, i.e., -1 refers to the last element of the array.$slicing _range (optional): This parameter refers to the range or limit point upto which the slicing is needed to be done. A negative value will indicate the count from the end of the string. Now, this can also be left blank. On leaving blank the function will slice through all the values as mentioned in the starting point right up to the end.preserve (optional): This parameter can take only two boolean parameters, i.e., either True or False. This will tell the function whether to preserve the keys or reset it. True refers to preserve the keys and false refers to reset the keys. False being the default value. Return Value: As already mentioned this function will return the selected or the sliced parts of the array.Below program illustrate the array_slice() function in PHP: In this program, we will be passing all the positive parameters along with the True value to preserve the keys. PHP <?php // PHP program to illustrate the // array_slice() function // Input array $array = array("ram", "krishna", "aakash", "gaurav", "raghav"); // Slice from pos 1 to pos 3 print_r(array_slice($array, 1, 3, true)); ?> Output: Array ( [1] => krishna [2] => aakash [3] => gaurav )Now let's try to observe the output by passing the same values as in above program, but with False as the value of the preserved key. PHP <?php // PHP program to illustrate the // array_slice() function // input array $array = array("ram", "krishna", "aakash", "gaurav", "raghav"); // Slice from pos 1 to pos 3 print_r(array_slice($array, 1, 3, false)); ?> Output: Array ( [0] => krishna [1] => aakash [2] => gaurav )Below program shows what happens when we dont give the range parameter: PHP <?php // PHP program to illustrate the // use of array_slice() // Input array $array = array("ram", "krishna", "aakash", "gaurav", "raghav"); // Slice from pos 1 to end print_r(array_slice($array, 1)); ?> Output: Array ( [0] => krishna [1] => aakash [2] => gaurav [3] => raghav )Below program illustrate the array_slice() function when we pass the negative parameter as our starting position: PHP <?php // PHP program to illustrate the // use of array_slice() // Input array $array = array("ram", "krishna", "aakash", "gaurav", "raghav"); // Slice from pos 3rd position to // the end of the array print_r(array_slice($array, -3)); ?> Output: Array ( [0] => aakash [1] => gaurav [2] => raghav )Below program shows what happens when we try to pass the negative parameters as both the starting point and the length or range: PHP <?php // PHP program to illustrate the // use of array_slice() // Input Array $array = array("ram", "krishna", "aakash", "gaurav", "raghav"); // Slice from pos 1 to end print_r(array_slice($array, -3, -2, true)); ?> Output: Array ( [2] => aakash ) Reference: http://php.net/manual/en/function.array-slice.php Comment More infoAdvertise with us Next Article PHP array_slice() Function chinmoy lenka Follow Improve Article Tags : Misc Web Technologies PHP PHP-array PHP-function +1 More Practice Tags : Misc Similar Reads PHP array_chunk() Function The array_chunk() function is an inbuilt function in PHP which is used to split an array into parts or chunks of given size depending upon the parameters passed to the function. The last chunk may contain fewer elements than the desired size of the chunk. Syntaxarray array_chunk( $array, $size, $pre 2 min read PHP array_combine() Function The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of t 2 min read PHP array_count_values() Function The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi 2 min read PHP array_diff_assoc() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares both the keys and values between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to there keys and values and returns the 2 min read PHP array_diff_key() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares the keys between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to their keys and returns the elements that are present 2 min read PHP array_diff_uassoc() Function The array_diff_uassoc() function is a built-in function in PHP and is used to get the difference between one or more arrays using an user-defined function to compare the keys. This function compares both the keys and values between one or more arrays to and returns the elements from the first array 4 min read PHP array_diff_ukey() Function The array_diff_ukey() function is an inbuilt function in PHP. It is used to compares the key two or more arrays using a user-defined function, and return an array, which is array1 and its not present any others array2, array3 or more... Syntax: array_diff_ukey($array1, $array2, $array3..., arr_diffu 4 min read PHP array_diff() function The array_diff() is an inbuilt function in PHP ans is used to calculate the difference between two or more arrays. This function computes difference according to the values of the elements, between one or more array and return differences in the form of a new array. This function basically returns a 2 min read PHP array_fill_keys() Function The array_fill_keys() function is a builtin function in PHP and is used to create a new array filled with the given keys and value provided as an array to the function. Syntax: array array_fill_keys ( $keys, $value ) Parameters: This function accepts two parameters, keys and their values to be prese 3 min read PHP array_fill() function The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value. Syntax: array_fill($start_index, $number_elements, $values) Parameter: The array_fill() function takes three parameters and ar 2 min read Like