How to remove duplicate values in PHP? [duplicate]

百般思念 提交于 2019-12-25 02:07:55

问题


I need help. Want to remove duplicate values from array in php.

$categoryTotal echoes 144 125 262 108 351 177 266 269 270 268 309 144 125 262 238 108

On using:

vardump() 

I get

Array ( [0] => 144 [1] => 125 [2] => 262 [3] => 108 [4] => 351 [5] => 177 [6] => 266 [7] => 269 [8] => 270 [9] => 268 [10] => 309 [14] => 238 ) 

I used sort to sort the values in ascending order I get:

108 108 125 125 144 144 177 238 262 262 266 268 269 270 309 351 

but then using $categoryTotal=array_unique($categoryTotal,SORT_NUMERIC); few values like 309 351 disappear.

Would like to know how to know out duplicate values from this array.


回答1:


Asked before.

$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)



回答2:


Use the array_unique() function, like this:

$noDuplicates = array_unique($categoryTotal);

Docs here.



来源:https://stackoverflow.com/questions/16063590/how-to-remove-duplicate-values-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!