Remove an Element at Given Index from Array in PHP Last Updated : 23 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Table of Content Using unset() FunctionUsing array_splice() FunctionUsing foreach LoopUsing unset() FunctionThe 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() FunctionThe 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 LoopA 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 ) Comment More infoAdvertise with us Next Article How to get elements in reverse order of an array in PHP ? B blalverma92 Follow Improve Article Tags : PHP 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 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 Like