How to delete an array element based on key in PHP?

31 Jul, 2021
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 elementprint_r($arr);  // Use unset() function delete// elementsunset($arr[2]);  // Display the array elementprint_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 Resultsprint_r($marks);   // Use unset() function to// delete elementsunset($marks["Ram"]);  echo "After delete the element <br>";  // Display Resultsprint_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
        )

)

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.
Picked
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to get parameters from a URL string in PHP?
Split a comma delimited string into an array in PHP
Download file from URL using PHP
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
How to get parameters from a URL string in PHP?
Split a comma delimited string into an array in PHP
How to pass JavaScript variables to PHP ?