Remove specific element from array PHP

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 calling unset().
  • Finally, if the key exists in the $fruits array, the unset() 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

  1. To Remove Elements or Values from Array PHP
  2. How to Convert String to Array in PHP
  3. Array Push and POP in PHP | PHP Tutorial
  4. PHP Search Multidimensional Array [key and value and return key]
  5. PHP Array to String Conversion – PHP Implode
  6. Array Functions In PHP
  7. Functions: Remove First Character From String PHP
  8. Remove Specific/Special Characters From String In PHP
  9. How to Replace First and Last Character From String PHP
  10. Reverse String in PHP
  11. remove duplicates from multidimensional array PHP
  12. PHP Remove Duplicate Elements or Values from Array PHP
  13. PHP Convert Array to Comma Separated String
  14. Compare Arrays Keys and Values PHP
  15. PHP Object to Array Convert using JSON Decode
  16. PHP Get Highest Value in Multidimensional Array
  17. Convert CSV to JSON PHP
  18. How to Check If the Variable is Empty in PHP

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?