How to Delete an Array Element based on Key in PHP? Last Updated : 05 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given an array containing elements. The task is to remove empty elements from the array based on the key in PHP.Example:Input: Array ( [0] => 'G' [1] => 'E' [2] => 'E' [3] => 'K' [4] => 'S' ) Key = 2Output: Array ( [0] => 'G' [1] => 'E' [3] => 'K' [4] => 'S' )Below are the approaches to deletean Array element based on key in PHP:Table of ContentUsing unset() FunctionUsing array_diff_key()Using unset() FunctionThe unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array. After removal the associated key and value does not change.Syntax:unset($variable)Parameter: This function accepts single parameter variable. It is required parameter and used to unset the element.Example: Delete an element from one dimensional array. php // PHP program to delete an array // element based on index // Declare arr variable // and initialize it $arr = array('G', 'E', 'E', 'K', 'S'); // Display the array element print_r($arr); // Use unset() function delete // elements unset($arr[2]); // Display the array element print_r($arr); ?> OutputArray ( [0] => G [1] => E [2] => E [3] => K [4] => S ) Array ( [0] => G [1] => E [3] => K [4] => S ) Using array_diff_key()This PHP script uses array_diff_key() to create a modified array $result by excluding elements with key 'b'. The output displays the array without the element 'b' => 'Banana', demonstrating selective removal based on keys.Example: This example shows the use of the above-mentioned approach. PHP // Sample array $array = array( 'a' => 'Apple', 'b' => 'Banana', 'c' => 'Cherry', 'd' => 'Date' ); // Delete element with key 'b' $result = array_diff_key($array, array('b' => true)); // Output the modified array print_r($result); ?> OutputArray ( [a] => Apple [c] => Cherry [d] => Date ) PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. Comment More infoAdvertise with us Next Article How to Delete an Array Element based on Key in PHP? S shruti1399 Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to Get All Keys of an Associative Array in PHP? Given an Associative array, the task is to get all the keys of the associative array in PHP. There are two approaches to get all keys from the associative array, these are - using array_keys(), and foreach loop. In this article, we will explore these approaches with examples.Table of ContentApproach 2 min read PHP Program to Delete Middle Element from an Array Given an array, the task is to delete the middle element of the array. If the array has an even number of elements, the middle element can be considered as the one closer to the start of the array (the lower middle).These are the following approaches:Table of ContentUsing array_splice() functionUsin 3 min read How to Replace an Element Inside Array in PHP ? Given an array containing some elements, the task is to replace an element inside the array in PHP. There are various methods to manipulate arrays, including replacing elements. Examples:Input: arr = [10, 20, 30, 40, 50], index_1 = 100Output: [10, 100, 30, 40, 50]Input: arr = ['GFG', 'Geeks', 'Hello 3 min read How to Create an Array of Given Size in PHP? This article will show you how to create an array of given size in PHP. PHP arrays are versatile data structures that can hold multiple values. Sometimes, it's necessary to create an array of a specific size, either filled with default values or left empty. Table of Content Using array_fill() Functi 3 min read Different Ways to Delete an Item From an Array in PHP Given an array containing some elements, the task is to Delete an item from an array using PHP. Below are the approaches to delete an item from an Array in PHP: Table of Content Using unset() FunctionUsing array_splice() FunctionUsing array_diff() FunctionUsing array_filter() FunctionUsing array_sea 3 min read Remove an Element at Given Index from Array in PHP 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() FunctionTh 2 min read How to get the First Element of an Array in PHP? Accessing the first element of an array in PHP is a common task, whether they are indexed, associative, or multidimensional. PHP provides various methods to achieve this, including direct indexing, and built-in functions.Below are the approaches to get the first element of an array in PHP:Table of C 4 min read PHP Program to Find Duplicate Elements from an Array Given an array containing some repeated elements, the task is to find the duplicate elements from the given array. If array elements are not repeated, then it returns an empty array.Examples:Input: arr = [1, 2, 3, 6, 3, 6, 1]Output: [1, 3, 6]Input: arr = [3, 5, 2, 5, 3, 9]Output: [3, 5]There are two 6 min read How to Remove Last Element from an Array in PHP ? Removing the last element from an array is a common operation in PHP. There are several approaches to achieve this. In this article, we will explore different methods to remove the last element from an array.Table of ContentUsing array_pop() FunctionUsing array_slice() FunctionUsing unset() Function 3 min read How to loop through an associative array and get the key in PHP? Associative arrays are used to store key-value pairs. For example, to store the marks of the different subject of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subjectâs names as the keys in our associative array, and the value 3 min read Like