compare two arrays of numbers and remove duplicates in php

南笙酒味 提交于 2019-12-08 07:35:43

问题


OK i have two groups of mobile numbers (from mysql) which i need to process, the problem is i need to remove duplicate numbers from the results.

Someone told me about "array_intersect" but I am not very good at these things and I don't see any good examples on the PHP website.

Any help or suggestions is appreciated thanks :)


回答1:


array_intersect isn't quite right — that finds numbers that are in both arrays

$uniques = array_unique(array_merge($array1, $array2));

This merges the two arrays together and then filters out all the unique results (with array_unique)




回答2:


Use the array_unique function.

$myArray = array(1, 1, 2, 3, 3, 5);
$myArray2 = array_unique($myArray);

http://php.net/manual/en/function.array-unique.php




回答3:


Put both lists into one array and then run it through array_unique() .




回答4:


As you wrote about using MySQL, better try using something like

SELECT DISTINCT phone_number FROM table

With DISTINCT each row in the resultset will be unique.




回答5:


Use the array_unique function. Here is an example:

$start = array(1,2,3,3,4,4,4,5);
$unique_result = array_unique($start);


来源:https://stackoverflow.com/questions/3945967/compare-two-arrays-of-numbers-and-remove-duplicates-in-php

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