How to Slice an Array in PHP? Last Updated : 04 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to help you do this task quickly.These are the following approaches: Table of ContentUsing array_slice() FunctionUsing a Custom FunctionUsing array_slice() FunctionThe array_slice() function is the standard way to slice an array in PHP. This function allows you to specify the starting index, the length of the slice, and whether to preserve the array keys.Syntax:array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): arrayExample: This example shows the creation of a custom function to slice an array. PHP <?php $input = [1, 2, 3, 4, 5, 6, 7]; $slice = array_slice($input, 2, 3); print_r($slice); ?> OutputArray ( [0] => 3 [1] => 4 [2] => 5 ) Using a Custom FunctionEven though array_slice() usually works well, you can write a custom function to slice an array by hand. This could be helpful when adding more logic to the slicing process or in learning scenarios.Syntax:function custom_slice(array $array, int $offset, int $length): array { $sliced_array = []; for ($i = $offset; $i < $offset + $length; $i++) { if (isset($array[$i])) { $sliced_array[] = $array[$i]; } } return $sliced_array;}Example: This example shows the creation of a custom function to slice an array. PHP <?php // Define the custom_slice() function function custom_slice(array $array, int $offset, int $length): array { $sliced_array = []; for ($i = $offset; $i < $offset + $length; $i++) { if (isset($array[$i])) { $sliced_array[] = $array[$i]; } } return $sliced_array; } // Now call the custom_slice() function $input = ['apple', 'banana', 'cherry', 'date', 'elderberry']; $slice = custom_slice($input, 1, 3); print_r($slice); ?> OutputArray ( [0] => banana [1] => cherry [2] => date ) Comment More infoAdvertise with us Next Article How to display array structure and values in PHP ? H heysaiyad Follow Improve Article Tags : PHP Similar Reads PHP array_slice() Function 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 (mandato 3 min read PHP array_splice() Function This inbuilt function of PHP is an advanced and extended version of array_slice() function, where we not only can remove elements from an array but can also add other elements to the array. The function generally replaces the existing element with elements from other arrays and returns an array of r 2 min read How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using 2 min read How to read each character of a string in PHP ? A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHPTable of ContentUsing str_split() method - The str_ 4 min read PHP array_shift() Function This inbuilt function of PHP removes the first element from an array and returns the value of the removed element. After the removal of the first element, the key of the remaining elements is modified and again re-numbered from the start, only if the keys are numerical. In other words, this function 2 min read How to Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', ' 2 min read Like