All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Remove Key From Array

Last Updated : Mar 11, 2024

PHP Remove Key From Array

In this article we will show you the solution of PHP remove key from array, if you want to remove an element from an array, you can use the "unset()" method in PHP.

This function removes the relevant key-value pair from an array after receiving an array and the key of the element that has to be deleted as inputs.

It's crucial to remember that this function does not reindex the array; it simply removes the element at the provided key.

In various situations, such as removing duplicate entries, erroneous or unnecessary data, or shrinking the array's size to enhance efficiency, it might be helpful to remove an element from an array.

Now move to the concept of php remove key from array.

Step By Step Guide On PHP Remove Key From Array :-

By providing the key of the element to be removed, the unset() function can be used to remove elements from an array.

Although it is a helpful function for eliminating certain elements from an array, care should be taken to prevent unintentionally erasing crucial information.

<?php
// Define an associative array
$fruits = array(
    "apple" => "red",
    "banana" => "yellow",
    "orange" => "orange",
    "grape" => "purple"
);
// Display the original array
echo "Original array: <br>";
print_r($fruits);
// Remove the "banana" element from the array
unset($fruits["banana"]);
// Display the modified array
echo "<br>Modified array: <br>";
print_r($fruits);
?>
  1. You can see that we wrote PHP code to delete the key from the array here.
  2. We define the $fruits array at the beginning of the code as an associative array with four entries, each of which consists of a key-value pair.
  3. The phrase "Original array:" continue to appear, and then the print_r() method is used to show the $fruits array's initial contents.
  4. After that, the "banana" member is taken out of the $fruits array using the unset() function.
  5. The array's name and the key of the element to be deleted are parameters for the unset() function.
  6. The fruit "banana" is taken out of the $fruits array because it is the key in this particular case.
  7. Using the print_r() function & an echo statement that shows the message "Modified array: ", the "banana" member is removed from the $fruits array, showing the modified contents of the array.
  8. The array's contents are then displayed in a human-readable format using the print_r() method.
  9. It displays the array's contents as a list of key-value pairs, having each pair appearing on a separate line.

Conclusion :-

As a result, we have successfully learned how to remove a key from an array in PHP.

We also discovered that the unset() function in PHP offers a quick and efficient way to eliminate a specific key from an associative array.

For organising data in a key-value format in PHP, associative arrays are a handy data structure.

The array's contents can be seen in a readable format by using the print_r() method.

To prevent mistakenly erasing important information when working with arrays, the unset() function must be used with care.

I hope this article on PHP remove key from array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪