php array_unique() function


  Translation results:

UK [juˈni:k] US [juˈnik]

adj. Only, only; unique, unique; unusual, special; extraordinary

php array_unique() functionsyntax

Function:Remove duplicate values ​​in the array

Syntax: array_unique(array)

Parameters:

ParametersDescription
arrayRequired. Specifies an array.
sortingtypeOptional. Specifies how array elements/items are compared. Possible values: SORT_STRING - Default. Compare items as strings. SORT_REGULAR - Arrange each item in regular order (Standard ASCII, does not change type) SORT_NUMERIC - Treat each item as a number. SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).

Description: First sort the values ​​as strings, then only retain the first encountered key name for each value, and then ignore all The key name behind. This does not mean that the first occurrence of the same value in an unsorted array will be preserved.

php array_unique() functionexample

<?php
$a=array("a"=>"php中文网","b"=>"西门","c"=>"php中文网");
print_r(array_unique($a));
?>

Run instance»

Click the "Run instance" button to view the online instance

Output:

Array ( [a] => php中文网 [b] => 西门 )


<?php
$b=array("1"=>"php中文网","2"=>"灭绝师太","c"=>"php中文网",'4' => "欧阳克");
print_r(array_unique($b));
?>

Run Instance»

Click the "Run Instance" button to view the online instance

Output:

Array ( [1] => php中文网 [2] => 灭绝师太 [4] => 欧阳克 )

Home

Videos

Q&A