PHP | Program to delete an element from array using unset() function Last Updated : 27 Apr, 2018 Comments Improve Suggest changes Like Article Like Report Given an array of elements, we have to delete an element from the array by using the unset() function. Examples: Input : $arr = array("Harsh", "Nishant", "Bikash", "Barun"); unset($arr[3]); Output : Array ( [0] => Harsh [1] => Nishant [2] => Bikash ) Input : $arr = array(1, 2, 6, 7, 8, 9); unset($arr[3]); Output : Array ( [0] => 1 [1] => 2 [2] => 6 [4] => 8 [5] => 9 ) unset() function: The function accepts a variable name as parameter and destroy or unset that variable. Approach: This idea to solve this problem using the unset function is to pass the array key of the respective element which we want to delete from the array as a parameter to this function and thus removes the value associated to it i.e. the element of an array at that index. Below programs illustrate the above approach: Program 1: php <?php $a = array("Harsh", "Bikash", "Nishant", "Barun", "Deep"); // unset command accepts 3rd index and // thus removes the array element at // that position unset($a[3]); print_r ($a); ?> Output: Array ( [0] => Harsh [1] => Bikash [2] => Nishant [4] => Deep ) Program 2: php <?php $a = array(1, 8, 9, 7, 3, 5, 4, ); // unset command accepts 3rd index and // thus removes the array element // at that position unset($a[5]); print_r ($a); ?> Output: Array ( [0] => 1 [1] => 8 [2] => 9 [3] => 7 [4] => 3 [6] => 4 ) Note: The array keys will not be reordered after using the unset() function. Comment More infoAdvertise with us Next Article PHP | Program to delete an element from array using unset() function A akash1295 Follow Improve Article Tags : Technical Scripter Web Technologies PHP Similar Reads 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 delete an element from an array using JavaScript ? The 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. In this article, we will discuss different ways to remove elements from the array. 5 min read PHP | Deleting an element from array using array_diff() Given an array, we have to delete an element from that array using the array_diff() function in PHP.Examples:Input : $arr = array(2, 8, 9, 7, 6, 5); $arr = array_diff($arr, array(9)); Output : Array ( [0] => 2 [1] => 8 [3] => 7 [4] => 6 [5] => 5 ) Input : $arr = array("shubham", "aksh 2 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 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 Like