How to remove strings from array in php

PHPz
Release: 2023-04-18 14:50:11
Original
840 people have browsed it

php removes strings from arrays

In the programming process, we often need to operate on arrays to process and convert data. In an array, sometimes we need to remove some specified strings to get the results we want.

In PHP programming, there are many ways to remove strings from arrays. This article will introduce a few of these methods and help you choose the one that best suits your needs.

Method 1: Use the array_filter function

The array_filter function is a very useful function in PHP, used to filter elements in an array. This function has two parameters, the first parameter is the array to be filtered, and the second parameter is the callback function used for filtering. In the callback function, we can specify filtering rules, for example, remove a specified string.

The following is an example:

$array = array("apple", "orange", "banana", "pear");
$remove_value = "orange";

$new_array = array_filter($array, function ($value) use ($remove_value) {
    return $value != $remove_value;
});

print_r($new_array);
Copy after login

Output result:

Array
(
    [0] => apple
    [2] => banana
    [3] => pear
)
Copy after login
Copy after login
Copy after login

In the above code, we define an array that contains some fruit names. We also define a string "orange" to be eliminated. Then we use the array_filter function to filter out the "orange" string in the array, and finally output a new array.

Method 2: Use the array_diff function

The array_diff function is also a very useful function in PHP, used to compare the differences between two arrays. This function accepts two or more arrays as arguments and returns an array containing the difference between the first array and the other arrays.

The following is an example:

$array1 = array("apple", "orange", "banana", "pear");
$array2 = array("orange");

$new_array = array_diff($array1, $array2);

print_r($new_array);
Copy after login

Output result:

Array
(
    [0] => apple
    [2] => banana
    [3] => pear
)
Copy after login
Copy after login
Copy after login

In the above code, we define two arrays, one of which contains some fruit names, and the other The string "orange" to be removed from the array. Then we use the array_diff function to compare the two arrays and finally output the new array.

Method 3: Use foreach loop

Using foreach loop is a relatively simple method, which can directly traverse the array and delete unnecessary elements.

The following is an example:

$array = array("apple", "orange", "banana", "pear");
$remove_value = "orange";

foreach ($array as $key => $value) {
    if ($value == $remove_value) {
        unset($array[$key]);
    }
}

print_r($array);
Copy after login

Output result:

Array
(
    [0] => apple
    [2] => banana
    [3] => pear
)
Copy after login
Copy after login
Copy after login

In the above code, we define an array that contains some fruit names. We also define a string "orange" to be eliminated. Then we use a foreach loop to traverse the array and delete the element if it is a string to be eliminated. Finally output the new array.

To sum up, PHP provides a variety of methods to remove strings from arrays. Different methods are suitable for different situations, and you can choose the method that works best for you based on your needs. No matter which method is used, you can process arrays more conveniently and quickly, making programming more efficient and simpler.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template