来自网址:Count number of a group array in array in php
我问问题:
$arr1 = [61,41,41,61,89,90]
$arr2 = [61,41]
$result = 2
//found 61 and 41 in $arr1 2 time;
//This mean : found 61 and 41 in $arr1[0] and $arr1[1]
//and found 61 and 41 in $arr1[3] and $arr1[2] again
//So $result = 2
我按照答案。 那是代码:
$arr1 = array(61,41,41,61,89,90);
$arr2 = array(61,41);
$count = array_count_values($arr1); //count values from arr1
$result = array();
foreach($arr2 as $row) {
$result[$row] = array_key_exists($row, $count) ? $count[$row] : 0;
}
echo min($result);
但它有一个错误。如果我假设
$arr1 = array(5,6,5,6,5,7);
$arr2 = array(5,5);
$result = 1;
//This mean : found 5 and 5 in $arr[0] and $arr[2]
// but is'not found 5 and 5 again
真正的结果是1.但是这个结果是3。 请帮我解决这个问题。
答案 0 :(得分:0)
您可以使用array-intersect()
和array_unique()
http://php.net/manual/en/function.array-intersect.php
http://php.net/manual/en/function.array-unique.php
$tmp = array_intersect($arr1, $arr2);
$tmp = array_unique($tmp);
$result = count($res);
答案 1 :(得分:0)
只需添加条件,因为在这种情况下你必须匹配两次
echo ($arr2[0] == $arr2[1]) ? floor($result[0] /2) : min($result);