How to remove duplicate values in an array
I want to remove duplicate 开发者_如何学运维values from an array.
Here is my array
$arr=array([0]=>123,[1]=>223,[2]=>323,[3]=>123,[4]=>223);
For removing duplicate values, I used array_unique()
function, but it still shows the same array.
Is there any method to solve this problem?
use array_unique()
<?php
$arr=array([0]=>123,[1]=>223,[2]=>323,[3]=>123,[4]=>223);
$result = array_unique($arr);
print_r($result);
?>
Your code works fine for me.
$arr = array(0 => 123, 1 => 223, 2 => 323, 3 => 123, 4 => 223);
var_dump(array_unique($arr));
Output
array(3) {
[0]=>
int(123)
[1]=>
int(223)
[2]=>
int(323)
}
CodePad.
Note that array_unique()
returns a new array, it doesn't take the array by reference, so you'll need to assign the returned array somewhere.
try this
$arrUnique = array_unique($arr);
print_r($arrUnique);
精彩评论