1<?php
2$fruits_list = array('Orange', 'Apple', ' Banana', 'Cherry', ' Banana');
3$result = array_unique($fruits_list);
4print_r($result);
5?>
6
7Output:
8
9Array ( [0] => Orange [1] => Apple [2] => Banana [3] => Cherry )
1
2<?php
3$input = array("a" => "green", "red", "b" => "green", "blue", "red");
4$result = array_unique($input);
5print_r($result);
6?>
7
8Array
9(
10 [a] => green
11 [0] => red
12 [1] => blue
13)
1<?php
2 // Syntax: array_unique(array $array, int $flags = SORT_STRING): array
3 // Desc: Removes duplicate values from an array
4 $arr = Array("red", "red", "green", "blue", "blue", "white");
5 echo "<pre>";
6 print_r($arr);
7 echo "</pre> <br><br>";
8
9 $arrUnique = array_unique($arr);
10
11 echo "<pre>";
12 print_r($arrUnique);
13 echo "</pre> <br><br>";
14
15 /* -------- output -----------
16 Array
17 (
18 [0] => red
19 [1] => red
20 [2] => green
21 [3] => blue
22 [4] => blue
23 [5] => white
24 )
25
26 Array
27 (
28 [0] => red
29 [2] => green
30 [3] => blue
31 [5] => white
32 )
33 */
34?>
1$counts = array_count_values($array);
2 $duplicate_title = array_filter($array, function ($value) use ($counts) {
3 return $counts[$value] > 1;
4 });
1let chars = ['A', 'B', 'A', 'C', 'B'];
2let uniqueChars = [...new Set(chars)];
3
4console.log(uniqueChars);
5