How to delete an array element based on key in PHP?
Given an array (One dimensional or multidimensional) and the task is to delete an array element based on key value.
Examples:
Input: Array
(
[0] => 'G'
[1] => 'E'
[2] => 'E'
[3] => 'K'
[4] => 'S'
)
Key = 2
Output: Array
(
[0] => 'G'
[1] => 'E'
[3] => 'K'
[4] => 'S'
)
Using unset() Function: The 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.
Program 1: 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); ?> |
Array
(
[0] => G
[1] => E
[2] => E
[3] => K
[4] => S
)
Array
(
[0] => G
[1] => E
[3] => K
[4] => S
)
Program 2: Delete an element from associative array.
<?php // PHP program to creating two // dimensional associative array $marks = array( // Ankit will act as key "Ankit" => array( // Subject and marks are // the key value pair "C" => 95, "DCO" => 85, ), // Ram will act as key "Ram" => array( // Subject and marks are // the key value pair "C" => 78, "DCO" => 98, ), // Anoop will act as key "Anoop" => array( // Subject and marks are // the key value pair "C" => 88, "DCO" => 46, ), ); echo "Before delete the element <br>"; // Display Results print_r($marks); // Use unset() function to // delete elements unset($marks["Ram"]); echo "After delete the element <br>"; // Display Results print_r($marks); ?> |
Before delete the element
Array ( [Ankit] => Array ( [C] => 95 [DCO] => 85 ) [Ram] => Array ( [C] => 78 [DCO] => 98 ) [Anoop] => Array ( [C] => 88 [DCO] => 46 ) ) After delete the element
Array ( [Ankit] => Array ( [C] => 95 [DCO] => 85 ) [Anoop] => Array ( [C] => 88 [DCO] => 46 ) )
Recommended Posts:
- PHP | Program to delete an element from array using unset() function
- Delete the first element of array without using shift() method in JavaScript
- Delete the array elements in JavaScript | delete vs splice
- How to delete last element from a map in C++
- How to repeat HTML element multiple times using ngFor based on a number?
- Difference between Memory based and Register based Addressing Modes
- How to change an element color based on value of the color picker value on click?
- How to change an element color based on value of the color picker value using onclick?
- How to delete an item or object from the array using ng-click ?
- How to move an array element from one array position to another in JavaScript?
- Difference between Preemptive Priority based and Non-preemptive Priority based CPU scheduling algorithms
- How to get the first element of an array in PHP?
- PHP | Second most frequent element in an array
- Searching in Array for a given element in Julia
- PHP | Deleting an element from array using array_diff()
- How to remove an element from an array in JavaScript?
- How to call the map method only if the element is an array?
- Removing Array Element and Re-Indexing in PHP
- How to append an element in an array in JavaScript?
- Find the min/max element of an Array using JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

