开发者

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜