How to remove duplicates from an array in PHP

3 Answers

0 votes
$arr = array("php"=>"99", "c"=>"108", "php"=>"99", "c++"=>"31", "c++"=>"31", "php"=>"99");
    
$arr = array_unique($arr);

print_r($arr);





/*
run:

Array
(
    [php] => 99
    [c] => 108
    [c++] => 31
)

*/

 



answered Jun 30, 2022 by avibootz
0 votes
$arr = [4, 7, 9, 0, 7, 4, 4, 5, 7, 9];
    
$arr = array_unique($arr);

print_r($arr);




/*
run:

Array
(
    [0] => 4
    [1] => 7
    [2] => 9
    [3] => 0
    [7] => 5
)

*/

 



answered Jun 30, 2022 by avibootz
0 votes
$arr = array(1, 1, "1", 1, 2, "3", 3, 3, "3", "4");
    
$arr = array_unique($arr);

print_r($arr);

var_dump($arr);




/*
run:

Array
(
    [0] => 1
    [4] => 2
    [5] => 3
    [9] => 4
)
array(4) {
  [0]=>
  int(1)
  [4]=>
  int(2)
  [5]=>
  string(1) "3"
  [9]=>
  string(1) "4"
}

*/

 



answered Jun 30, 2022 by avibootz

Related questions

...