How to Switch the First Element of an Arrays Sub Array in PHP? Last Updated : 26 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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', 'i']]Using a Temporary VariableIn this approach, we store the first element of the first sub-array in a temporary variable and then replace the first element of the first sub-array with the first element of the last sub-array and at last assign the value stored in the temporary variable to the first element of the last sub-array.Example: Below is the implementation of the above approach to switch the first element of each sub-array in a multidimensional array. PHP <?php // Define the array $array = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'] ]; // Check if there are at least two sub-arrays if (count($array) >= 2) { // Store the first element // of the first sub-array $temp = $array[0][0]; // Switch the first element of the first // sub-array with the first // element of the last sub-array $array[0][0] = $array[count($array) - 1][0]; $array[count($array) - 1][0] = $temp; } // Print the modified array print_r($array); ?> OutputArray ( [0] => Array ( [0] => g [1] => b [2] => c ) [1] => Array ( [0] => d [1] => e [2] => f ...Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Switch the First Element of an Arrays Sub Array in PHP? M mishraaabcf9 Follow Improve Article Tags : PHP PHP-Questions php Similar Reads How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar 6 min read Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing 3 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 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 check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP 2 min read Like