我想计算数组中组数组的数量。实施例
$arr1 = [61,41,41,61,89,90]
$arr2 = [61,41]
$result = 2 //found 61,41 in $arr1 2 time;
或示例
$arr1 = [89,61,41,41,61,90]
$arr2 = [61,41,89]
$result = 1 //found 61,41,89 in $arr1 1 time;
如何编写代码或概念?
答案 0 :(得分:1)
我希望这会对你有帮助..
$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);
$ arr2 = [61,41]输出:2
$ arr2 = [61,41,89]输出:1
答案 1 :(得分:1)
$arr1 = array(61,41,41,61,89,90);
$arr2 = array(61,41);
$occurrences = min(
array_count_values(array_intersect($arr1, $arr2)) + array_fill_keys($arr2, 0)
);
可以说是一个有点模糊的解决方案,但只是一个表达式。返回整个$arr2
集合在$arr1
中出现的数字。