PHP array_unique() Function

PHP array_unique() Function is used to remove duplicate values from an array. PHP array_unique() function is PHP built-in function.

Syntax :

array_unique(array $array, int $flags = SORT_STRING);

Parameter,

$array : Required. It is input array.

$flags  :  Optional. It is sorting type flags.It specifies how to compare the array elements/items.
              SORT_STRING - Default. Compare items as strings
              SORT_REGULAR - Compare items normally (don't change types)
              SORT_NUMERIC - Compare items numerically
              SORT_LOCALE_STRING - Compare items as strings based on current locale.

 

Return : It returns array containing unique values of an array. An array_unique() Function removes all duplicate values except first appearance value.

All values keep same preserved keys.

Let's see below example to understand php array_unique() Function in details.

Example :

<?php
$array = array(1=>"html",2=>"css",3=>"php",4=>"java",5=>"mysql",6=>"php",7=>"asp");
echo "<br><br>array_unique function removes duplicate value of php<br>";
$array_unique = array_unique($array); // Default sorting type is SORT_STRING.
print_r($array_unique);
$array=array("1",2,3,"2", 5,1,"1",2,"5","2");
echo "<br><br>array_unique function removes duplicate values. Sorting type is SORT_NUMERIC <br>";
$array_unique = array_unique($array,SORT_NUMERIC); // Sorting type is SORT_NUMERIC.
print_r($array_unique);

?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

90058