There are three common methods for deduplicating arrays in php: 1. The "array_unique()" function can remove duplicate values in the array and return a new array; 2. Use a loop to delete duplicates, use for Or a foreach loop traverses the entire array to determine whether there are duplicates and delete them; 3. Use the "array_flip()" and "array_keys()" functions. The "array_flip()" function exchanges the keys and values of the array, and "array_keys()" Get all key names.
Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.
There are three common ways to remove duplicates from arrays in php:
1. Use the array_unique() function
array_unique() function to remove duplicates from the array of repeated values and returns a new array.
$array = array("a" => "apple", "b" => "banana", "c" => "apple"); $unique_array = array_unique($array); print_r($unique_array);
The output result is:
Array ( [a] => apple [b] => banana )
2. Use a loop to delete duplicate items
Use a for or foreach loop to traverse the entire array and judge Check if there are duplicates and delete them. This method works well for small arrays, but can be time-consuming for large arrays.
$array = array("a" => "apple", "b" => "banana", "c" => "apple"); foreach ($array as $key => $value) { $count = 0; foreach ($array as $k => $v) { if ($v == $value) { $count++; if ($count > 1) { unset($array[$k]); } } } } print_r($array);
Output:
Array ( [a] => apple [b] => banana )
3. Use the array_flip() and array_keys() functions
Use the array_flip() function to convert the keys and values of the array Swap, and use array_keys() to get all key names. Since the keys are unique, this effectively creates a set of results that does not contain duplicate items.
$array = array("a" => "apple", "b" => "banana", "c" => "apple"); $unique_array = array_flip(array_flip($array)); print_r($unique_array);
The output is:
Array ( [a] => apple [b] => banana )
These methods are only a few methods that can achieve array deduplication. There are other methods such as using array_count_values(), array_reduce(), etc. For developers to master according to actual needs.
The above is the detailed content of There are several ways to deduplicate arrays in php. For more information, please follow other related articles on the PHP Chinese website!
if($res){
return json_encode(array('code'=>1,'msg'=>'成功'));
}else{
return json_encode(array('code'=>0,'msg'=>'失败'));
}
}
public function
}
if($res){
return json_encode(array('code'=>1,'msg'=>'成功'));
}else{
return json_encode(array('code'=>0,'msg'=>'失败'));
}
}
public function
}
if($res){
return json_encode(array('code'=>1,'msg'=>'成功'));
}else{
return json_encode(array('code'=>0,'msg'=>'失败'));
}
}
public function
}
if($res){
return json_encode(array('code'=>1,'msg'=>'成功'));
}else{
return json_encode(array('code'=>0,'msg'=>'失败'));
}
}
public function
}
if($res){
return json_encode(array('code'=>1,'msg'=>'成功'));
}else{
return json_encode(array('code'=>0,'msg'=>'失败'));
}
}
public function
}
if($res){
return json_encode(array('code'=>1,'msg'=>'成功'));
}else{
return json_encode(array('code'=>0,'msg'=>'失败'));
}
}
public function
}