PHP Program to Split Array and Add First Part to the End | Set 2 Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array, the task is to split it from a specified position, and move the first part of array and add to the end. Examples: Input : Arr = [ 12, 10, 5, 6, 52, 36 ] k = 2 Output : Arr = [ 5, 6, 52, 36, 12, 10 ] Explanation : Split from index 2 and first part {12, 10} add to the end. Input : Arr = [ 3, 1, 2 ] k = 1 Output : Arr = [ 1, 2, 3 ] Explanation : Split from index 1 and first part add to the end.A O(n*k) solution is discussed here. This problem can be solved in O(n) time using the reversal algorithm discussed below:Reverse array from 0 to n - 1 (where n is size of the array). Reverse array from 0 to n - k - 1. Reverse array from n - k to n - 1. PHP <?php // PHP program to Split the array and // add the first part to the end /* Function to reverse arr[] from index start to end*/ function rvereseArray(&$arr, $start, $end) { while ($start < $end) { $temp = $arr[$start]; $arr[$start] = $arr[$end]; $arr[$end] = $temp; $start++; $end--; } } // Function to print an array function printArray(&$arr, $size) { for ($i = 0; $i < $size; $i++) echo $arr[$i] . " "; } /* Function to left rotate arr[] of size n by k */ function splitArr(&$arr, $k, $n) { rvereseArray($arr, 0, $n - 1); rvereseArray($arr, 0, $n - $k - 1); rvereseArray($arr, $n - $k, $n - 1); } /* Driver program to test above functions */ $arr = array( 12, 10, 5, 6, 52, 36 ); $n = sizeof($arr); $k = 2; // Function calling splitArr($arr, $k, $n); printArray($arr, $n); ?> Output5 6 52 36 12 10 Please refer complete article on Split the array and add the first part to the end | Set 2 for more details! Comment More infoAdvertise with us Next Article How to Switch the First Element of an Arrays Sub Array in PHP? K kartik Follow Improve Article Tags : PHP rotation array-rearrange Reverse Practice Tags : Reverse Similar Reads Javascript Program to Split the array and add the first part to the end | Set 2 Given an array and split it from a specified position, and move the first part of array add to the end.  Examples:  Input : arr[] = {12, 10, 5, 6, 52, 36} k = 2 Output : arr[] = {5, 6, 52, 36, 12, 10} Explanation : Split from index 2 and first part {12, 10} add to the end . Input : arr[] = {3, 1, 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 PHP | Print the last value of an array without affecting the pointer We are given an array with key-value pair, and we need to find the last value of array without affecting the array pointer. Examples: Input : $arr = array('c1' => 'Red', 'c2' => 'Green', 'c3' => 'Blue', 'c4' => 'Black') Output : Black Input : $arr = array('p1' => 'New York', 'p2' = 2 min read PHP | Separate odd and even elements from array without using loop You are given an array of n elements in PHP. You have to separate the elements from the array based on the elements are odd or even. That is, print odd array and even array separately without traversing the original array or using any loop. Examples: Input : array(2, 5, 6, 3, 0) Output : Odd array: 3 min read How to merge the first index of an array with the first index of second array? The task is to merge the first index of an array with the first index of another array. Suppose, an array is array1 = {a, b, c} and another array is array2 = {c, d, e} if we perform the task on these arrays then the output will be result array { [0]=> array(2) { [0]=> string(1) "a" [1]=> st 3 min read PHP | Program to move (key,value) pair upwards or downwards Given an array with key, value pair, we need to move specific value upwards or downwards using the key. Examples: Input : $continents = array( 'First' => 'Asia', 'Second' => 'Europe', 'Third' => 'North America', 'Fourth' => 'South America', ); move_to_up($continents, 'Third'); Output : A 2 min read Like