PHP Array Unique
In PHP if we need to remove duplicate values then we can use array_unique() function. The functionality of it is to accept an array and returns another array without duplicate values.
General description of array_unique() is given below:
| General Format | array array_unique(array $array [, int $sort_flags=sort_string] ) | |
| Parameters | array: Input array | sort_flags: The second optional parameter is
used to compare items as follows
|
| Return Value | Another array without duplicate values | |
Example 1:
<?php
$array1=array("a"=>"Apple","b"=>"Banana","o"=>"Orange","b"=>"Apple","b"=>"Apple"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array1); echo"<br/><b>New array without duplicate values is:</b><br/>";print_r(array_unique(
$array1));?>
Output:
Initially the values of $array1 is:
array(3) {
["a"]=>
string(5) "Apple"
["b"]=>
string(5) "Apple"
["o"]=>
string(6) "Orange"
}
New array without duplicate values is:
Array
(
[a] => Apple
[o] => Orange
)
Example 2:
<?php
$array1=array("Apple","Banana","Orange","Apple","Apple"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array1); echo"<br/><b>New array without duplicate values is:</b><br/>";print_r(array_unique(
$array1));?>
Output:
Initially the values of $array1 is:
array(5) {
[0]=>
string(5) "Apple"
[1]=>
string(6) "Banana"
[2]=>
string(6) "Orange"
[3]=>
string(5) "Apple"
[4]=>
string(5) "Apple"
}
New array without duplicate values is:
Array
(
[0] => Apple
[1] => Banana
[2] => Orange
)
Example 3:
<?php
$array1=array("a"=>"Apple","b"=>"Banana","o"=>"Orange","a"=>"Apple","a"=>"Apple"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
var_dump(
$array1); echo"<br/><b>New array without duplicate values is:</b><br/>";print_r(array_unique(
$array1));?>
Output:
Initially the values of $array1 is:
array(3) {
["a"]=>
string(5) "Apple"
["b"]=>
string(6) "Banana"
["o"]=>
string(6) "Orange"
}
New array without duplicate values is:
Array
(
[a] => Apple
[b] => Banana
[o] => Orange
)
Example 4:
<?php
$array1=array(1=>"b",0=>"a","num"=>array(0=>"a",1=>"b",0=>"a"),2=>"b"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
print_r(
$array1); echo"<br/><b>New array without duplicate values is:</b><br/>";print_r(array_unique(
$array1));?>
Output:
Initially the values of $array1 is:
Array
(
[1] => b
[0] => a
[num] => Array
(
[0] => a
[1] => b
)
[2] => b
)
New array without duplicate values is:
Array
(
[1] => b
[0] => a
[num] => Array
(
[0] => a
[1] => b
)
)
Example 5:
<?php
$array1=array(1=>"b",0=>"a","num"=>array(0=>"a",1=>"b",0=>"a"),1=>"b"); echo "<br/><b>Initially the values of \$array1 is:</b><br/>";
print_r(
$array1); echo"<br/><b>New array without duplicate values is:</b><br/>";print_r(array_unique(
$array1));?>
Output:
Initially the values of $array1 is:
Array
(
[1] => b
[0] => a
[num] => Array
(
[0] => a
[1] => b
)
)
New array without duplicate values is:
Array
(
[1] => b
[0] => a
[num] => Array
(
[0] => a
[1] => b
)
)