Home > Backend Development > PHP Problem > How to remove strings from array in php

How to remove strings from array in php

PHPz
Release: 2023-04-26 09:59:36
Original
682 people have browsed it

In PHP, we often need to operate on arrays. One common operation is to remove strings from an array. This article will introduce how to use functions and methods in PHP to achieve this operation.

1. Use the function array_filter()

The function array_filter() is a built-in function in PHP that can remove some values ​​from the array. Its usage is as follows:

array_filter(array $input, callable $callback = "?"): array
Copy after login

Among them, the parameter $input is the array that needs to be processed, and the parameter $callback is an optional callback function used to determine which array elements are retained.

The following is an example:

$my_array = [1, 2, 'hello', 'world'];

$filtered_array = array_filter($my_array, function($element) {
    return (!is_string($element)); //返回true表示要保留该元素,返回false表示要去掉该元素
});

print_r($filtered_array); //输出:Array ( [0] => 1 [1] => 2 )
Copy after login

In the above example, $filtered_array only contains integer elements 1 and 2.

2. Use the function array_map()

The function array_map() is also a PHP built-in function. It can convert an array into another array and at the same time automatically perform operations on each element. Define operations. You can use array_map() to remove strings from an array. The specific method is as follows:

array_map(callable $callback , array $array , array ...$arrays): array
Copy after login

Among them, the parameter $callback is a callback function used to operate each element in the array. The parameter $array is the array to be processed, and the parameter $arrays is an optional other array.

The following is an example:

$my_array = [1, 2, 'hello', 'world'];

$filtered_array = array_map(function($element) {
    if(is_string($element)) { //如果是字符串,则返回null,表示要去掉该元素
        return null;
    } else { //否则返回元素本身
        return $element;
    }
}, $my_array);

print_r($filtered_array); //输出:Array ( [0] => 1 [1] => 2 [2] => [3] => )
Copy after login

In the above example, $filtered_array only contains integer elements 1 and 2.

3. Use array_intersect()

The function array_intersect() is used to compare two or more arrays and return an array containing elements that exist in all arrays. We can use this function to remove strings from an array.

The following is an example:

$my_array = [1, 2, 'hello', 'world'];

$filtered_array = array_intersect($my_array, array_filter($my_array, 'is_int'));

print_r($filtered_array); //输出:Array ( [0] => 1 [1] => 2 )
Copy after login

In the above example, $filtered_array contains integer elements 1 and 2 that do not contain any strings.

Summary

In PHP, we can use a variety of methods to remove strings from arrays. This article introduces some commonly used methods, including the function array_filter(), the function array_map() and the function array_intersect(). Using these functions can help us perform array operations more conveniently.

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