
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to delete an array element based on key in PHP?
To delete an array element based on a key in PHP, the code is as follows−
Example
<?php $arr = array( " John ", "Jacob ", " Tom ", " Tim "); echo "Array with leading and trailing whitespaces...
"; foreach( $arr as $value ) { echo "Value = $value
"; } echo "
Comma separated list...
"; print_r(implode(', ', $arr)); $result = array_map('trim', $arr); echo "
Updated Array...
"; foreach( $result as $value ) { echo "Value = $value
"; } unset($result[1]); echo "
Updated Array...
"; foreach( $result as $value ) { echo "Value = $value
"; } ?>
Output
This will produce the following output−
Array with leading and trailing whitespaces... Value = John Value = Jacob Value = Tom Value = Tim Comma separated list... John , Jacob , Tom , Tim Updated Array... Value = John Value = Jacob Value = Tom Value = Tim Updated Array... Value = John Value = Tom Value = Tim
Example
Let us now see another example −
<?php $marks = array( "kevin" => array ( "physics" => 95, "maths" => 90, ), "ryan" => array ( "physics" => 92, "maths" => 97, ), ); echo "Marks for kevin in physics : " ; echo $marks['kevin']['physics'] . "
"; echo "Marks for ryan in maths : "; echo $marks['ryan']['maths'] . "
"; unset($marks["ryan"]); echo "Marks for ryan in maths : "; echo $marks['ryan']['maths'] . "
"; ?>
Output
This will produce the following output. Now, an error would be visible since we deleted the element and trying to access it−
Marks for kevin in physics : 95 Marks for ryan in maths : 97 Marks for ryan in maths : PHP Notice: Undefined index: ryan in /home/cg/root/6985034/main.php on line 25
- Related Articles
- How to delete an element from an array in PHP and re-index the array?
- How to delete element from an array in MongoDB?
- PHP program to delete an element from the array using the unset function
- Search and update array based on key JavaScript
- How to delete/remove an element from a C# array?
- Appending a key value pair to an array of dictionary based on a condition in JavaScript?
- How to filter documents based on an array in MongoDB?
- In PHP, how can I add an object element to an array?
- How to delete rows of an R data frame based on string match?
- C program to delete an array element using Pointers
- How to get the first element of an array in PHP?
- Compute the truth value of an array AND to another array element-wise based on conditions in Numpy
- Compute the truth value of an array OR to another array element-wise based on conditions in Numpy
- How to find an element based on a data-attribute value in jQuery?
- How to delete n-th element of array in MongoDB?

Advertisements