Home  >  Article  >  Backend Development  >  How to remove specified string from array in php

How to remove specified string from array in php

PHPz
PHPzOriginal
2023-04-19 10:06:49507browse

In PHP, array is a very useful data type, which can be used to store a set of ordered data. There are many ways to use PHP arrays, such as adding, deleting, modifying, traversing, etc. However, in actual programming, we often encounter situations where we need to delete specific strings from an array. This article will show you how to quickly and efficiently delete strings from an array using some functions in PHP.

  1. Use array_filter() function

We can use PHP's array_filter() function to filter elements in the array, which can accept a callback function (callback) as The second parameter. A Boolean value must be returned in this callback function. If true is returned, the element is retained, otherwise it is filtered out.

The following is an example that demonstrates how to use the array_filter() function to delete strings in an array:

$arr = array('dog', 'cat', 'fish', 'bird');
$remove = 'fish';
$arr = array_filter($arr, function($value) use ($remove) { 
    return $value !== $remove;
});
print_r($arr);

The output result is:

Array
(
    [0] => dog
    [1] => cat
    [3] => bird
)

In this example, we define An array $arr is created, which contains several strings. Next, we define a string $remove that needs to be deleted. We use the array_filter() function to filter the array $arr, and use an anonymous function as a callback function to execute the filtering conditions of the elements. In this anonymous function, we pass the $remove variable into the callback function using the use keyword and then use the !== operator to compare the array elements and $remove, if not equal, returns true, otherwise returns false. Finally, we replace the original array with the modified array $arr.

  1. Using the array_diff() function

We can also use PHP’s array_diff() function to delete specific elements in the array. This function can accept two or more arrays as parameters, and the returned value is a new array containing the difference elements in the two arrays.

The following is an example that demonstrates how to use the array_diff() function to delete elements in an array:

$arr = array('dog', 'cat', 'fish', 'bird');
$remove = array('fish');
$arr = array_diff($arr, $remove);
print_r($arr);

The output result is:

Array
(
    [0] => dog
    [1] => cat
    [3] => bird
)

In this example, we define An array $arr, which contains several strings. Then we define a $remove array to store the elements that need to be deleted. Finally, we use the array_diff() function to compare the two arrays $arr and $remove and return a new array of differing elements. Eventually, the new array replaces the original array, and the deleted elements will not appear in the new array.

  1. Using foreach loop

In addition to using PHP’s own functions, we can also use foreach loop to traverse the array and determine whether an element is equal to the specified string to delete this element.

The following is an example that demonstrates how to use a foreach loop to delete elements in an array:

$arr = array('dog', 'cat', 'fish', 'bird');
$remove = 'fish';
foreach ($arr as $key => $value) {
    if ($value === $remove) {
        unset($arr[$key]);
    }
}
print_r($arr);

The output result is:

Array
(
    [0] => dog
    [1] => cat
    [3] => bird
)

In this example, we still define a An array $arr containing several strings. Next, we define the element $remove that needs to be removed. Then we use a foreach loop to traverse the array $arr and check whether each element is equal to $remove. If equal, use the unset() function to delete the element. . Finally, we output the modified array.

  1. Using the array_map() function

Finally, we can also use PHP’s array_map() function to apply a callback function to each element of the array. The callback function must return a modified value and at the end a new array.

The following is an example that demonstrates how to use the array_map() function to delete elements in an array:

$arr = array('dog', 'cat', 'fish', 'bird');
$remove = 'fish';
$arr = array_map(function($value) use ($remove) {
    return $value === $remove ? null : $value;
}, $arr);
$arr = array_filter($arr);
print_r($arr);

The output result is:

Array
(
    [0] => dog
    [1] => cat
    [3] => bird
)

In this example, we will array_map () function is used as a way to delete elements from an array. First, we define an array $arr, which contains several strings. Next, we define the element $remove that needs to be removed. In the array_map() function, we use an anonymous function to define the filter condition of the element. If the element is equal to $remove, null is returned, otherwise the original value is returned. Finally, we use the array_filter() function to filter null values ​​in the array, producing a new array in which the specified element has been removed.

Summary

This article introduces four methods to delete specific elements in an array in PHP: using the array_filter() function, using the array_diff() function, using the foreach loop and using the array_map() function . No matter which method you use, you can quickly and efficiently delete elements from an array. In actual programming, choosing different solutions to delete array elements according to needs can improve the efficiency and readability of the code.

The above is the detailed content of How to remove specified string from 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