Home > Backend Development > PHP Problem > How to remove a string from an array in php

How to remove a string from an array in php

PHPz
Release: 2023-04-19 13:41:49
Original
660 people have browsed it

In PHP, sometimes we want to remove certain strings from an array. This situation is very common when writing web applications, because the user may enter incomplete or incorrect strings. In this case, we need to remove these unnecessary characters so that the program can work properly.

PHP provides several methods to remove specific strings in an array. Below we describe these methods and provide some sample code to help you understand how to use them.

  1. Using the array_diff() function

The array_diff() function is a very useful function in PHP. It can compare two or more arrays and return an A new array containing only elements not contained in the first array. In this case, we can use the array_diff() function to remove a specific string from an array.

The following is a simple sample code that demonstrates how to use the array_diff() function to remove a specific string in an array:

// 定义一个数组
$array = array('apple', 'banana', 'orange', 'pineapple');

// 指定要去掉的字符串
$remove = 'banana';

// 使用array_diff()函数去掉字符串
$result = array_diff($array, array($remove));

// 输出结果
print_r($result);
Copy after login

In the above code, we define an array, It contains 4 fruit names. Then, we specify the string to be removed as "banana" and remove the string using the array_diff() function and an array containing the string to be removed. Finally, we output the results through the print_r() function. Running the above code, the output is as follows:

Array
(
    [0] => apple
    [2] => orange
    [3] => pineapple
)
Copy after login
Copy after login

It can be seen that the "banana" string has been removed from the array, leaving only the other three elements.

  1. Using the array_filter() function

In addition to the array_diff() function, PHP also provides another useful function array_filter(). It can filter out some elements in the array based on the conditions you set. We can use this to remove specific strings from an array.

The following is a sample code that demonstrates how to use the array_filter() function to remove specific strings in an array:

// 定义一个数组
$array = array('apple', 'banana', 'orange', 'pineapple');

// 指定要去掉的字符串
$remove = 'banana';

// 使用array_filter()函数去掉字符串
$result = array_filter($array, function($value) use ($remove) {
    return ($value != $remove);
});

// 输出结果
print_r($result);
Copy after login

In the above code, we define an array that contains 4 fruit names. Then, we specify the string to be removed as "banana" and use the array_filter() function and a callback function to filter out the string. Finally, we output the results through the print_r() function. Running the above code, the output is as follows:

Array
(
    [0] => apple
    [2] => orange
    [3] => pineapple
)
Copy after login
Copy after login

It can also be seen that the "banana" string has been removed from the array, leaving only the other three elements.

  1. Using a foreach loop

If you don’t want to use PHP’s built-in functions, you can also use a foreach loop to remove specific strings in an array. The following is a sample code that demonstrates how to use a foreach loop to remove a specific string in an array:

// 定义一个数组
$array = array('apple', 'banana', 'orange', 'pineapple');

// 指定要去掉的字符串
$remove = 'banana';

// 使用foreach循环去掉字符串
foreach($array as $key => $value) {
    if($value == $remove) {
        unset($array[$key]);
    }
}

// 重新索引数组
$array = array_values($array);

// 输出结果
print_r($array);
Copy after login

In the above code, we define an array containing 4 fruit names. Then, we specify the string to be removed as "banana" and use a foreach loop to check whether each element is equal to the string to be removed one by one. If they are equal, we use the unset() function to remove the element. Finally, we reindexed the array using the array_values() function so that it starts counting again at 0. Finally, we output the results through the print_r() function. Running the above code, the output is as follows:

Array
(
    [0] => apple
    [1] => orange
    [2] => pineapple
)
Copy after login

It can also be seen that the "banana" string has been removed from the array, leaving only the other three elements.

Summary

There are many ways to remove specific strings in an array in PHP. You can use the array_diff() function, array_filter() function or foreach loop to achieve this. You can choose different methods according to your actual needs. No matter which method you choose, it should be easy to get rid of the unwanted strings from the array.

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