To remove a specific element from an array in PHP, you can use the unset() function.
Syntax:
unset($array[$index]);
Example:
$array = array(“a”=>”Apple”, “b”=>”Banana”, “c”=>”Cherry”);
unset($array[“b”]);
// Result
Array
(
[a] => Apple
[c] => Cherry
)
If you want to delete/remove/unset specific elements or multiple elements from an array by key, value. In this tutorial, you will learn how to use the unset()
function in PHP to remove single or multiple php array elements by key, value, and index.
How to Delete Specific Element From Array By Key Value and Index using Unset() in PHP
By following these examples, you will learn how to use the unset()
function in PHP to remove specific array elements by key, value, and index.
- Removing an array element by key
- Removing an array element by value
- Removing an array element by index
- Unset Multiple Keys from Array
Removing an array element by key
To remove an array element by key, you need to know the key of the element you want to remove.
Here’s an example:
<?php $fruits = array("apple" => "red", "banana" => "yellow", "grape" => "purple"); unset($fruits["banana"]); print_r($fruits); ?>
In above-given code, you have an array of fruits with their colors. And you want to remove the element with the key “banana” from this array. Then you need to use the unset()
function and pass in the key of the element to remove.
Removing an array element by value
To remove an array element by value, you need to search for the value and get its key. Then you can remove it from array by it’s value.
Here’s an example:
<?php $fruits = array("apple", "papaya", "grape"); $key = array_search("papaya", $fruits); if ($key !== false) { unset($fruits[$key]); } print_r($fruits); ?>
In the above-given code, you have same array, which you have used in first example. Then, you can use the array_search() function to search for the value and get its key. If the value is found, use the unset()
function to remove the element with that key.
Removing an array element by index
To remove an array element by index, you need to know the index of the element, which you want to remove.
Here’s an example:
<?php $fruits = array("apple", "banana", "grape"); unset($fruits[1]); print_r($fruits); ?>
Unset Multiple Keys from Array
Sometimes, you may need to remove multiple elements from an array. So, you can use a loop to iterate through the array and call unset()
on each key you want to remove.
Here’s an example:
$fruits = array( "apple" => 0.50, "banana" => 0.25, "cherry" => 1.00, "grape" => 0.75, "orange" => 0.30 ); $keys_to_remove = array("banana", "orange"); foreach ($keys_to_remove as $key) { if (array_key_exists($key, $fruits)) { unset($fruits[$key]); } }
Here’s a breakdown of the above-given code:
- First, an associative array
$fruits
is created, which contains a list of fruits and their respective prices. Each fruit is a key-value pair, where the fruit name is the key, and the price is the value. - Next, an array
$keys_to_remove
is defined, which contains the keys (i.e., fruit names) of the elements that you want to remove from the$fruits
array. - Then, a
foreach
loop is used to iterate through the$keys_to_remove
array. On each iteration, the loop assigns the current element to the variable$key
. - Inside the loop,
array_key_exists()
the function is used to check if the current key exists in the$fruits
array. This is important because if you try to unset a key that does not exist in the array, PHP will not raise an error, but will simply do nothing. Therefore, we need to ensure that the key exists before callingunset()
. - Finally, if the key exists in the
$fruits
array, theunset()
the function is called to remove the element from the array. This effectively removes the element with the corresponding key from the$fruits
array.
After running this code, the $fruits
the array will look like this:
Array ( [apple] => 0.5 [cherry] => 1 [grape] => 0.75 )
Conclusion
The unset()
a function is a powerful tool in PHP for removing array elements by key, value, and index. By following the examples in this tutorial, you can learn how to use unset()
to manipulate arrays in your PHP applications effectively. Remember that unset()
removes the element from the array completely, and the remaining elements will be reindexed automatically.
Recommended PHP Tutorials
- To Remove Elements or Values from Array PHP
- How to Convert String to Array in PHP
- Array Push and POP in PHP | PHP Tutorial
- PHP Search Multidimensional Array [key and value and return key]
- PHP Array to String Conversion – PHP Implode
- Array Functions In PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- remove duplicates from multidimensional array PHP
- PHP Remove Duplicate Elements or Values from Array PHP
- PHP Convert Array to Comma Separated String
- Compare Arrays Keys and Values PHP
- PHP Object to Array Convert using JSON Decode
- PHP Get Highest Value in Multidimensional Array
- Convert CSV to JSON PHP
- How to Check If the Variable is Empty in PHP