Swipe First & Last Element of an Array in PHP
Last Updated :
23 May, 2024
Swapping the first and last elements of an array is a common operation that can be useful in various scenarios, such as rearranging data or preparing it for specific algorithms.
Below are the approaches to swipe the first and last element of an array in PHP:
Using Temporary Variable
One of the simplest methods to swap the first and last elements of an array is to use a temporary variable. This method ensures that the values are swapped without losing any data.
Example: In this approach, we store the first element in a temporary variable, then assign the last element to the first position, and finally place the temporary variable's value in the last position.
PHP
<?php
// Swap First and Last Array Elements
// using Temperory Variable
function swapElements(&$arr) {
$n = count($arr);
$temp = $arr[0];
$arr[0] = $arr[$n - 1];
$arr[$n - 1] = $temp;
}
// Driver code
$arr = [10, 20, 30, 40, 50];
swapElements($arr);
print_r($arr);
?>
OutputArray
(
[0] => 50
[1] => 20
[2] => 30
[3] => 40
[4] => 10
)
Using List() and Array Destructuring
PHP 7 introduced array destructuring, which can be used to swap the first and last elements more succinctly.
Example: In this approach, the list() construct is used to swap the values directly. The elements are destructured and reassigned in a single statement, making the code more readable and concise.
PHP
<?php
// Swap First and Last Array Elements
// using Array Destructuring
function swapElements(&$arr) {
$n = count($arr);
list($arr[0], $arr[$n - 1]) = [$arr[$n - 1], $arr[0]];
}
// Driver code
$arr = [10, 20, 30, 40, 50];
swapElements($arr);
print_r($arr);
?>
OutputArray
(
[0] => 50
[1] => 20
[2] => 30
[3] => 40
[4] => 10
)
Using Array Functions
We can also use PHP's built-in array functions to achieve the swap.
Example: Here, we use array_shift() to remove the first element and array_pop() to remove the last element. We then use array_unshift() to add the last element to the beginning and array_push() to add the first element to the end. This method utilizes PHP's array manipulation functions for clarity.
PHP
<?php
// Swap First and Last Array Elements
// using PHP Functions
function swapElements(&$arr) {
$n = count($arr);
$first = array_shift($arr);
$last = array_pop($arr);
array_unshift($arr, $last);
array_push($arr, $first);
}
// Driver code
$arr = [10, 20, 30, 40, 50];
swapElements($arr);
print_r($arr);
?>
OutputArray
(
[0] => 50
[1] => 20
[2] => 30
[3] => 40
[4] => 10
)
Similar Reads
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
Find the Second Largest Element in an Array in PHP We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further opera
3 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 Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function
5 min read
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