How to Slice an Array in PHP? Last Updated : 04 Sep, 2024 Comments Improve Suggest changes 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 Slice an Array 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 How to get specific key value from array in PHP ? In this article, we will see how to get specific key values from the given array. PHP array is a collection of items that are stored under keys. There are two possible types of keys: strings and integers. For any type of key, there is a common syntax to get a specific value by key â square brackets. 3 min read How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i 4 min read How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements 4 min read How to Break Foreach Loop in PHP? Breaking a foreach loop consists of terminating the execution of the loop prematurely based on a certain condition. In this article, we will explore and learn two approaches to breaking for each loop in PHP.Below are the approaches to break for each loop in PHP:Table of ContentUsing break KeywordUsi 3 min read Like