Home >Backend Development >PHP Problem >How to remove specified value from array and reset array in PHP

How to remove specified value from array and reset array in PHP

PHPz
PHPzOriginal
2023-04-18 09:47:15830browse

Arrays in PHP are often used to store a set of related data. In the process of processing this data, we often need to remove some of the values, or retain only the required values ​​​​according to certain conditions. At this time, we need to use the array operation functions in PHP, which include functions to remove the specified value of the array and reset the array. This article will introduce in detail how to remove the specified value of the array and reset the array in PHP.

1. Remove the specified value from the array

In PHP, we can use the array_diff function to remove the specified value from the array. The array_diff function will reorganize all the values ​​in the array except the specified value into a new array. The syntax is as follows:

array array_diff (array $array1, array $array2 [, array $...])

Among them, array1 is the array to be compared, array2 and the following parameters are is an array of values ​​to be removed. The function will return a new array that contains all the values ​​in array1 but not in other parameters. For example:

$array1 = array(1, 2, 3, 4, 5);
$array2 = array(3, 5);
$result = array_diff($array1, $array2);
print_r($result);

The output result is:

Array
(
    [0] => 1
    [1] => 2
    [3] => 4
)

In the above example, we first define an array $array1 containing five numbers from 1 to 5, and then define an array $array2, where Contains the values ​​3 and 5 to be removed. We use the array_diff function to remove 3 and 5 in $array1, and the return result is a new array $result, which only contains the three values ​​​​1, 2 and 4.

2. Reset the array key name

After removing the specified value from the array, the original key name in the array will also be removed. Therefore, we need to reassign a new key name to the array. , to ensure the continuity of its key names. We can use the array_values ​​function to reassign array keys. The array_values ​​function returns a new array that contains all the values ​​in the original array, and its corresponding key names are automatically optimized starting from 0. For example:

$array = array(1 => 'one', 2 => 'two', 3 => 'three');
$result = array_values($array);
print_r($result);

The output result is:

Array
(
    [0] => one
    [1] => two
    [2] => three
)

In the above example, we defined an array $array, which contains three elements whose key names are 1, 2 and 3 respectively. . We use the array_values ​​function to reset its array key name and return a new array $result, where the key names are 0, 1 and 2 respectively, and the corresponding values ​​are 'one', 'two' and ' in the original array. three'.

3. Remove the specified value from the array and reset the array

Sometimes, we need to integrate the first two steps, that is, remove the specified value from the array and ensure the continuity of the array key names. . For this purpose, we can use the following code:

$array = array(1, 2, 3, 4, 5);
$remove = array(3, 5);
$result = array_values(array_diff($array, $remove));
print_r($result);

The output result is:

Array
(
    [0] => 1
    [1] => 2
    [2] => 4
)

In the above code, we use the array_diff function to remove 3 and 5 in the array $array element, then use the array_values ​​function to reset its key name, and assign the result to the $result array.

Summary:

This article introduces the method of removing the specified value of the array and resetting the array in PHP, including removing the specified value function array_diff and resetting the array key name function array_values. These functions are very useful when processing arrays. If you need to deduplicate the array, remove null values, remove certain specified values, etc., then these functions will be very useful. I hope this article can help you solve practical problems.

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