‘apple’, ‘b’ => ‘banana’, ‘c’ => ‘coconut’);
// Value to remove
$value = ‘banana’;
// Check if value exists in array
if(($key = array_search($value, $array)) !== false) {
// Remove from array
unset($array[$key]);
}
// Output array
print_r($array);
// Output
Array ( [a] => apple [c] => coconut )
If there is a value in an array. And you have to remove it from an array. So you can remove the value from the array with the help of array_search(), unset(), foreach(), and is_array().
In this tutorial, you will learn how to remove a value if it exists from one-dimensional, two-dimensional, and multidimensional arrays in PHP.
How to Remove Value from Array if Exists in PHP
There are three different methods for different types of array to remove value from an these array if exists in PHP:
- Removing Value from One-Dimensional Array
- Removing Value from Two-Dimensional Array
- Removing Value from Multidimensional Array
Removing Value from One-Dimensional Array
To remove a value from a one-dimensional array, you can use the array_search()
function to search for the value and then use the unset()
function to remove it.
Here’s an example:
<?php $array = array("apple", "banana", "cherry", "date"); $search = "banana"; $key = array_search($search, $array); if ($key !== false) { unset($array[$key]); } print_r($array); ?>
In the above example an array of fruits taken. To remove the Banana value from this array. For this, first, the value was found with the method of array_search. Then removed the value from array by unset method.
Removing Value from Two-Dimensional Array
To remove a value from a two-dimensional array, we can use a loop to iterate over each sub-array and use the same technique as in the one-dimensional case to remove the value.
Here’s an example:
<?php $array = array( array("apple", "banana", "cherry"), array("date", "elderberry", "fig"), array("grape", "honeydew", "kiwi") ); $search = "banana"; foreach ($array as $key => $subarray) { $subkey = array_search($search, $subarray); if ($subkey !== false) { unset($array[$key][$subkey]); } } print_r($array); ?>
Now as an example, have taken two-dimensional array. In which fruits are named. And out of this also banana has to be removed. For this, first, iterate the foreach () loop. After that, the value to be removed from the array is searched using the array search method. After this, an if condition was used and the array was removed from the unset method.
Removing Value from Multidimensional Array
To remove a value from a multidimensional array, you can use a recursive function that traverses the array and removes the value using the same technique as in the two-dimensional case.
<?php $array = array( array( array("apple", "banana", "cherry"), array("date", "elderberry", "fig"), array("grape", "honeydew", "kiwi") ), array( array("lemon", "mango", "orange"), array("peach", "quince", "raspberry"), array("strawberry", "tangerine", "watermelon") ) ); $search = "banana"; function removeValue(&$array, $search) { foreach ($array as $key => &$value) { if (is_array($value)) { removeValue($value, $search); } else { $subkey = array_search($search, $array); if ($subkey !== false) { unset($array[$subkey]); } } } } removeValue($array, $search); print_r($array); ?>
In this example, a function has been created to remove the values from the multidimensional array. In this function, foreach() loop, array_search(), and unset() methods are used to remove value from multidimensional array.
Finally, call the removeValue() function with the array and the search value as parameters, and use the print_r() function to print the modified array.
This function will reduce like a recursive function. until it removes the given value from the array. Till then this function call will keep happening.
Conclusion
In conclusion, removing a value from an array in PHP is a common task that can be accomplished using the array_search()
function to find the index of the value and the unset()
function to remove it. For multidimensional arrays, a recursive function can be used to traverse the array and remove the value.