How To Remove Specific Element From Array in PHP

How To Remove Specific Element From Array in PHP

Remove specific elements from array in PHP. In this tutorial, you will learn how to remove or delete specific elements from php array by key, value, and index in PHP.

How to Delete Specific Element From Array By Key, Value and Index 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 used to remove array elements by key, value, and index. By following the examples in this tutorial, you can learn how to manipulate arrays in your PHP applications effectively using unset() function. 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. remove duplicates from multidimensional array PHP
  11. PHP Remove Duplicate Elements or Values from Array PHP
  12. PHP Convert Array to Comma Separated String
  13. Compare Arrays Keys and Values PHP
  14. PHP Object to Array Convert using JSON Decode
  15. Convert CSV to JSON PHP
  16. How to Check If the Variable is Empty in PHP

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *