All TalkersCode Topics

Follow TalkersCode On Social Media

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

PHP Remove Value From Array

Last Updated : Mar 11, 2024

PHP Remove Value From Array

In this article we will show you the solution of PHP remove value from array, a built-in PHP method called array_search() enables programmers to look for a certain value in an array and extract the associated key.

An array value is normally removed from it using this function together with unset().

The array_search() function finds a value in an array and returns the key that matches it. The two inputs are the value that you want to search and an array to search in.

The function returns the key of the matched element if the value was found.

The unset() function can be used to delete an element from the array once the key of the matching element has been found.

This method is especially helpful for removing a specific value from a PHP associative or indexed array.

The keys of an associative array are strings, while the keys of an indexed array are numeric values.

We'll talk about the concept of removing a value from an array in PHP.

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

<?php
// Sample indexed array
$array1 = array(1, 2, 3, 4, 5);
// Search value and delete
if(($key = array_search(4, $array1)) !== false) {
    unset($array1[$key]);
}
print_r($array1);
echo "<br>";
// Sample eassociative array
$array2 = array("a" => "Apple", "b" => "Ball", "c" => "Cat");
// Search value and delete
if(($key = array_search("Cat", $array2)) !== false) {
    unset($array2[$key]);
}
print_r($array2);
?>
  1. As you can see, we have written PHP code to remove a value from an array.
  2. The first line of our code creates two arrays: $array1, which is an indexed array with the elements 1, 2, 3, 4, and 5, and $array2, which is an associative array with the keys "a," "b," and "c," and the associated values "Apple," "Ball," and "Cat."
  3. Then, we use the array_search() function to look in $array1 for the value 4.
  4. If the value is located, the array_search() method returns the element's array key, which is kept in the $key variable.
  5. If the value is found, the array's element with the same key is deleted using the unset() function and the!== false comparison.
  6. After that, the elements of $array1 following the deletion are shown using the print_r() method.
  7. After that, $array2 is searched for the value "Cat" using the array_search() function.
  8. If the value is located, the array_search() method returns the element's array key, which is kept in the $key variable.
  9. If the value is found, the unset() method is used to remove the element from the array with the matched key using the!== false check.
  10. After the deletion, the contents of $array2 are shown using the print_r() function.

Conclusion :-

Thus, the concept of removing a value from an array in PHP has been effectively taught.

Additionally, we discovered that the array_search() method in PHP may be used to find a given value in an array and its related key.

The array_search() function can help you rapidly find and delete a specific value from your array whether you're working with a tiny or huge array.

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪