Deletes the implementation code of repeated elements in the PHP array.
The previous article describes how to delete the header, tail, and arbitrary elements in the PHP array. This article describes how to delete repeated elements in the array using the array_unique () function.
array_unique()
Function, sorts the values of array elements as strings, and then retains only the first key name for each value. Ignoring all the key names that follow it is to delete repeated elements in the array,
The syntax format is as follows:
Array arry_unique (array)
The parameter array is the input array.
The following example causes the array_unique () function to delete repeated elements in the array. The sample code is as follows:
<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ array_push = array ("PHP 文"", "www.php.cn", "www.php.cn ", "PHP from entry to entry"); // defines the array array_push ($ array_push, "Baidu", "www.php.cn"); // adds the element print_r ($ array_push ); echo "<br>"; $ result = array_unique ($ array_push); // Delete the repeated elements in the array print_r ($ result); // output the deleted array?>
Output result:
array_unique()
The function is only applicable to one-dimensional arrays and does not apply to multi-dimensional arrays. However, you can use array_unique () as the value in a two-dimensional array.
The following example uses the array_unique () function to delete duplicate elements in a two-dimensional array. The 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);?>
Output result:
The preceding figure shows how to delete the details of repeated elements in the PHP array. For more information, see other related articles of helper's house!