How can I remove an element of an array based on a key in PHP?

PHP

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 the function unset() : function unset() is used to remove an item from an array. The unset function is used to destroy any other variable, and in the same way it is used to remove any element of the array. This unset command takes an array key as input and removes that element from the array. After deletion, the associated key and value are not changed.Syntax:
unset($variable)
Parameter: this function takes a variable with one parameter. This is a required parameter and is used to reset an item.Program 1:Remove an item from a one-dimensional array.
// PHP program to delete array
// element based on index 
// Declare the arr variable
// and initialize it $arr = array ( `G` , `E` , ` E` , `K` , ` S` );
 
// Show array element print_r ( $arr );  
// Use the unset() delete function
// elements unset ( $arr [2]);  
// Show array element print_r ( $arr );  
?>
Exit:
Array ([ 0] = > G [1] = > E [2] = > E [3] = > K [4] = > S) Array ([0] = > G [1] = > E [3] = > K [4] = > S)
Program 2:Remove an element from an associative array.  
// PHP program for creating two
// dimensional associative array $marks = array ( // Ankit will act as a key "Ankit" = > array ( // Topic and ratings // key-value pair " C " = > 95, "DCO" = > 85, ), // Ram will act as a key "Ram" = > array ( // Topic and ratings // key-value pair " C " = > 78, "DCO" = > 98, ), // Anoop will act as a key "Anoop" = > array ( // Topic and ratings // key-value pair " C " = > 88, "DCO" = > 46, ), ); echo " Before delete the element < br > " ;  
// Show results print_r ( $marks );  
// Use the unset() function for
// remove elements unset ( $marks [ "Ram" ]); echo " After delete the element < br > " ;  
// Show results print_r ( $marks );  
?>
Exit:
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))




Get Solution for free from DataCamp guru