How to remove specified value from php array

PHPz
Release: 2023-04-23 17:06:28
Original
519 people have browsed it

In PHP development, we often need to add, delete, modify and check arrays. Among them, deleting specified elements in an array is a common requirement. This article will introduce how to use PHP functions to achieve this operation.

There are two common ways to delete arrays in PHP: delete specified elements through the unset function, or use the array_filter function to filter out specified elements. The use of these two methods will be explained in detail below.

Use the unset function to delete specified elements

The unset function is a special function in PHP used to delete variables (including array elements). Its syntax is as follows:

unset($array[$key]);

Among them, $array is the array to be operated on, and $key is the subscript of the array element to be deleted.

The following is a simple example:

// 定义一个数组
$fruits = array('apple', 'banana', 'cherry', 'date', 'elderberry');

// 删除数组中下标为2的元素
unset($fruits[2]);

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

After executing the above code, the output result is as follows:

Array
(
    [0] => apple
    [1] => banana
    [3] => date
    [4] => elderberry
)
Copy after login

As can be seen from the above results, the subscript in the array is 2 The element (i.e. 'cherry') has been removed.

It should be noted that if you want to delete multiple elements, you can use multiple parameters in the unset function. For example:

// 定义一个数组
$fruits = array('apple', 'banana', 'cherry', 'date', 'elderberry');

// 删除数组中下标为1和3的元素
unset($fruits[1], $fruits[3]);

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

After executing the above code, the output result is as follows:

Array
(
    [0] => apple
    [2] => cherry
    [4] => elderberry
)
Copy after login

As can be seen from the above results, the elements with subscripts 1 and 3 in the array (i.e. 'banana' and 'date ') has been deleted.

Use the array_filter function to filter specified elements

The array_filter function is a built-in function in PHP, which is used to filter elements in an array. Its syntax is as follows:

array_filter($array, $callback);

Among them, $array is the array to be filtered, and $callback is a callback function used to define filtering rules.

The following is a simple example:

// 定义一个数组
$fruits = array('apple', 'banana', 'cherry', 'date', 'elderberry');

// 定义过滤回调函数,匹配以字母'a'开头的元素
function starts_with_a($value)
{
    return (substr($value, 0, 1) == 'a');
}

// 过滤数组中以字母'a'开头的元素
$filtered_fruits = array_filter($fruits, 'starts_with_a');

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

After executing the above code, the output result is as follows:

Array
(
    [0] => apple
)
Copy after login

As can be seen from the above results, the letter 'a in the array Elements starting with ' (i.e. 'apple') have been filtered out.

It should be noted that if you want to filter out multiple elements, you can use multiple conditions in the callback function to filter. For example:

// 定义一个数组
$fruits = array('apple', 'banana', 'cherry', 'date', 'elderberry');

// 定义过滤回调函数,过滤掉长度小于5的元素和以字母'd'开头的元素
function filter_fruits($value)
{
    return (strlen($value) >= 5 && substr($value, 0, 1) != 'd');
}

// 过滤数组中长度小于5的元素和以字母'd'开头的元素
$filtered_fruits2 = array_filter($fruits, 'filter_fruits');

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

After executing the above code, the output result is as follows:

Array
(
    [0] => banana
    [2] => cherry
    [4] => elderberry
)
Copy after login

As can be seen from the above results, the elements in the array whose length is less than 5 and the elements starting with the letter 'd' (i.e. 'apple' and 'date') have been filtered out.

This article introduces two common ways to delete specified elements in an array in PHP: using the unset function and using the array_filter function. Both methods have their own advantages and disadvantages, and developers can choose the appropriate method to implement array deletion operations based on actual needs.

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