我有 2 個陣列:
$first = array(1,1,2,3,4);
$second = array(1,2,3,4,5);
當我使用$count = array_intersect($first, $second);, 和count($count);匹配項時,它顯示 5,導致 1 相交兩次。我需要得到 4,它不計算重復項。
我怎樣才能在 php 中實作它?
提前致謝。
uj5u.com熱心網友回復:
很簡單:首先確保你的陣列是唯一的,然后應用array_intersect:
$count = count(array_intersect(array_unique($first), array_unique($second)));
在3v4l上重現案例。
uj5u.com熱心網友回復:
另外,在找到更好的答案之前,我想添加我已經弄清楚的這些錐體。
$result = array_intersect($first, $second);
$diff = array_diff($result, $second);
if(!empty($diff)) {
$result = array_unique($result);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322245.html
上一篇:如何對這個資料框架重新排序?
