Our last article talked about "How to delete the head, tail, and any element in a PHP array". In this article, we talk about deleting duplicate elements in the array through the array_unique() function.
array_unique() function sorts the values of the array elements as strings, and then only retains the first key name for each value and ignores all subsequent key names, which is to delete duplicate elements in the array,
The syntax format is as follows:
array arry_unique(array array)
The parameter array is the input array.
The following example uses the array_unique() function to delete duplicate elements in the array. The specific example code is as follows:
<?php header("Content-Type:text/html; charset=utf-8"); $array_push = array("PHP中文网","m.sbmmt.com", "m.sbmmt.com", "PHP 从入门到精通");//定义数组 array_push($array_push,"百度一下","m.sbmmt.com");//添加元素 print_r($array_push); echo "<br>"; $result=array_unique($array_push);//删除数组中重复的元素 print_r($result); //输出删除后的数组 ?>
The output result is:
## The #array_unique() function only applies to one-dimensional arrays, not multi-dimensional arrays. However, you can use array_unique() to add values in a two-dimensional array. The following example uses the array_unique() function to delete duplicate elements of a two-dimensional array. The specific code is as follows:,<?php $array = array(array(1,1,2),array(2,3,4,3)); print_r($array); $temp_array = array(); foreach($array as $key=>$value){ $temp_array[$key] = array_unique($value); } $array = $temp_array; echo "<br>"; print_r($array); ?>
Array statistical functions: count(), array_count_values() and array_unique()"
The above is the detailed content of How to remove duplicate elements in PHP array. For more information, please follow other related articles on the PHP Chinese website!