Home > Article > Backend Development > How to remove duplicate data in php
php method to remove duplicate data: 1. Use the "array_unique" function to directly remove duplicate values in an array; 2. Use the "array_flip" function and the "array_keys" function to remove duplicate data.
Recommendation: "PHP Video Tutorial"
Two kinds of PHP to remove duplicate data from arrays Method
1. Directly use the array_unique function
Use the array_unique function to directly remove duplicate values in an array, retaining only the first occurrence of the duplicate values and Its corresponding key value
Detailed instructions can be found in the PHP manual
Example 1:
$input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); 此时输出: Array ( [a] => green [0] => red [1] => blue )
Note: In the array_unique function, if int(3) and string( 1) When '3', the function will regard the two values as the same value, and will only retain the value that appears first
Example 2:
$input = array(4, "4", "3", 4, 3, "3"); $result = array_unique($input); var_dump($result);
At this time, it will output:
array(2) { [0] => int(4) [2] => string(1) "3" }
2. Use the array_flip function and the array_keys function together
It is very convenient to use the array_unique function, but there is one thing, the array_unique function will not change the original key value that is retained. This In some cases, it is difficult to operate a new array. At this time, you can use the array_flip function and array_keys function
array_flip function
to swap the values and key values of the specified array. The keys become values, and the values in the original array become keys, and if the same value appears multiple times, the last key becomes its value and all others are lost.
For example:
$trans = array("a" => 1, "b" => 1, "c" => 2); $trans = array_flip($trans); print_r($trans);
The output at this time is:
Array ( [1] => b [2] => c )
array_keys function
array_keys function can return some or all key names in the array, this It is easier to understand. If serch_value is specified, the key value of the specified query value is returned.
For example:
$array = array(0 => 100, "color" => "red"); print_r(array_keys($array)); $array = array("blue", "red", "green", "blue", "blue"); print_r(array_keys($array, "blue")); $array = array("color" => array("blue", "red", "green"), "size" => array("small", "medium", "large")); print_r(array_keys($array));
At this time, the output will be:
Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size )
After introducing the two functions After use, when you encounter an array with a lot of repeated data, and want to eliminate the repeated data in the middle and start counting the key values from '0', you can first use the array_flip function on the function, and then use the array_keys function on the new array. , you can achieve
Supplement: When the editor introduced the array_keys function, it suddenly occurred to me that there should be a function that can take out the values of all arrays. After checking, there is indeed an array_value function, which can return all the values of an array. The value is recorded into a new array, so that the key value can be counted from '0'. Therefore, if you want to achieve the purpose of this article, you can also use the array_unique function first, and then use the array_value function on it.
The above is the detailed content of How to remove duplicate data in php. For more information, please follow other related articles on the PHP Chinese website!