Home  >  Article  >  Backend Development  >  How to delete specified value in array in php

How to delete specified value in array in php

PHPz
PHPzOriginal
2023-03-29 11:28:50668browse

In PHP, array is an important data type that can save multiple values ​​and retrieve them based on key values. But sometimes we need to delete a specified value from an array. In this case, we need to use the array function provided by PHP.

In PHP, you can use the unset() function to delete an array element, but it can only delete a single element. If you want to delete multiple specified elements, you need to use the array_filter() function.

The array_filter() function is a very commonly used PHP array function. It can filter the elements in the array and return the elements that meet the conditions. The syntax of this function is as follows:

array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) : array

Among them, $array is the array to be filtered, $callback is the filter function, $flag is the flag parameter, 0 means retaining the original key name (default), 1 means reset Key name.

If the $callback parameter is not specified, the array_filter() function will delete all values ​​equivalent to false in the array, including 0, null, empty string and false. If the $callback function is specified, the function returns a new array whose elements are the elements whose return value is true after the $callback function is executed.

Below we use the array_filter() function to delete the specified value in the array:

function removeValueFromArray($array, $value) {
    return array_values(array_filter($array, function($item) use ($value) {
        return $item !== $value;
    }));
}

$array = array('apple', 'banana', 'orange', 'pear');
$value = 'banana';
$result = removeValueFromArray($array, $value);
print_r($result);

In the above example, we defined a function named removeValueFromArray(), which accepts two Parameters: $array represents the array of elements to be deleted, $value represents the value of the element to be deleted.

In the function, we use the array_filter() function to filter out elements equal to $value in the array, then use the array_values() function to re-index the result array, and finally return the result.

Through the above code, we have completed the operation of deleting the specified element in the array.

Summary:

In PHP, deleting the value specified in the array is a common operation. We can use the unset() function to delete a single element and the array_filter() function to delete multiple elements. When using the array_filter() function, you need to specify the conditions of the filter function to delete the specified elements.

The above is the detailed content of How to delete specified value in array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn