How to remove duplicate values from an array in PH

2018-12-31 08:12发布

How can I remove duplicate values from an array in PHP?

21条回答
伤终究还是伤i
2楼-- · 2018-12-31 08:33

That's a great way to do it. Might want to make sure its output is back an array again. Now you're only showing the last unique value.

Try this:

$arrDuplicate = array ("","",1,3,"",5);

foreach (array_unique($arrDuplicate) as $v){
  if($v != "") { $arrRemoved[] = $v; }
}
print_r ($arrRemoved);
查看更多
临风纵饮
3楼-- · 2018-12-31 08:34

Use array_unique().

Example:

$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
查看更多
临风纵饮
4楼-- · 2018-12-31 08:34

Use array_values(array_unique($array));

array_unique: for unique array array_values: for reindexing

查看更多
忆尘夕之涩
5楼-- · 2018-12-31 08:37

We can create such type of array to use this last value will be updated into column or key value and we will get unique value from the array...

$array = array (1,3,4,2,1,7,4,9,7,5,9);
    $data=array();
    foreach($array as $value ){

        $data[$value]= $value;

    }

    array_keys($data);
    OR
    array_values($data);
查看更多
人间绝色
6楼-- · 2018-12-31 08:38

The only thing which worked for me is:

$array = array_unique($array, SORT_REGULAR);
查看更多
忆尘夕之涩
7楼-- · 2018-12-31 08:39
$arrDuplicate = array ("","",1,3,"",5);
 foreach(array_unique($arrDuplicate) as $v){
  if($v != "" ){$arrRemoved = $v;  }}
print_r($arrRemoved);
查看更多
登录 后发表回答