PHP | Deleting an element from array using array_diff() Last Updated : 24 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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", "akshay", "vishal", "sweta"); $arr = array_diff($arr, array("akshay")); Output : Array ( [0] => shubham [2] => vishal [3] => sweta )array_diff(). The array_diff() function accepts two or more than two arguments and returns an array containing values from the first array which are not present in other arrays.Approach: The idea to solve this problem is we will pass two arrays to the array_diff() function as parameters. The first array in the parameter will be the array from which we want to delete the element. The second array will contain a single element which we want to delete from the first array. Finally, we will store the resultant array returned by the array_diff() function in the input array.Below programs illustrate the above approach: Program 1: php <?php $arr = array(2, 8, 9, 7, 6, 5); // returns the array after removing // the array value 9 $arr = array_diff($arr, array(9)); print_r ($arr); ?> Output: Array ( [0] => 2 [1] => 8 [3] => 7 [4] => 6 [5] => 5 ) Program 2: php <?php $arr = array("Harsh", "Nishant", "Akshay", "Barun", "Bikash"); // returns the array after removing // the array value "Akshay" $arr = array_diff($arr, array("Akshay")); print_r ($arr); ?> Output: Array ( [0] => Harsh [1] => Nishant [3] => Barun [4] => Bikash ) Comment More infoAdvertise with us Next Article PHP | Deleting an element from array using array_diff() A akash1295 Follow Improve Article Tags : Technical Scripter Web Technologies PHP Similar Reads 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 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 PHP | Program to delete an element from array using unset() function 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); 2 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 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