Change language

How can I remove an element of an array based on a key in 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
"
;  
// Show results print_r ( $marks );  
// Use the unset() function for
// remove elements unset ( $marks [ "Ram" ]); echo " After delete the element
"
;  
// 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))

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method