Remove an Element at Given Index from Array in PHP
Last Updated :
23 May, 2024
Given an array and an index, the task is to remove the element at a given index from an array in PHP.
Below are the approaches to remove an element at a given index from an array using PHP:
Using unset() Function
The unset() function is used to remove an element at a specific index. This function effectively removes the element but leaves the array keys unchanged.
Example: In this approach, unset($array[$index]) removes the element at the specified index. Note that the array keys are not re-indexed, so you may need additional steps if you require a contiguous array.
PHP
<?php
function removeElement($arr, $index) {
if (array_key_exists($index, $arr)) {
unset($arr[$index]);
}
return $arr;
}
// Driver code
$arr = [1, 2, 3, 4, 5];
$index = 2;
$updatedArr = removeElement($arr, $index);
print_r($updatedArr);
?>
OutputArray
(
[0] => 1
[1] => 2
[3] => 4
[4] => 5
)
Using array_splice() Function
The array_splice() function removes a portion of an array and can be used to remove an element at a specific index, while also re-indexing the array.
Example: Here, array_splice($array, $index, 1) removes one element at the specified index and re-indexes the array, making it contiguous again.
PHP
<?php
function removeElement($arr, $index) {
if (array_key_exists($index, $arr)) {
array_splice($arr, $index, 1);
}
return $arr;
}
// Driver code
$arr = [1, 2, 3, 4, 5];
$index = 2;
$updatedArr = removeElement($arr, $index);
print_r($updatedArr);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
)
Using foreach Loop
A manual method involves using a foreach loop to iterate over the array and rebuild it without the specified element.
Example: This approach involves creating a new array and adding elements that are not at the specified index. The resulting array is contiguous and does not have gaps.
PHP
<?php
function removeElement($arr, $index) {
$newArray = [];
foreach ($arr as $key => $value) {
if ($key != $index) {
$newArray[] = $value;
}
}
return $newArray;
}
// Driver code
$arr = [1, 2, 3, 4, 5];
$index = 2;
$updatedArr = removeElement($arr, $index);
print_r($updatedArr);
?>
OutputArray
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
)
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
Removing Array Element and Re-Indexing in PHP In order to remove an element from an array, we can use unset() function which removes the element from an array, and then use array_values() function which indexes the array numerically automatically. Function Usedunset(): This function unsets a given variable. Syntax:void unset ( mixed $var [, mix
2 min read
PHP | Remove duplicate elements from Array You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array. Examples: Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3} Output : array ( [6] => 2 [7] => 3 [4] => 1 [5] => 6 ) Input : array[] = {4, 2, 7, 3, 2, 7, 3} Output : arr
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