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 // 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 <?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 Like