将单个数组的值与多个系列的数组进行比较,然后回显结果

Compare values from a single array with multiple series of arrays, then echo the result

使用以下脚本,我可以计算 10 个不同数组中每个数字的 频率 ,然后回显按频率 类 对数字排序的结果(例如. 出现一次的值:x, y, z 出现两次的值:a, b, c, ....等)

<?php $set1 = ['23', '11', '52', '33', '1', '4'];
$set2 = ['66', '70', '55', '8', '22', '1'];
$set3 = ['38', '21', '52', '51', '53', '9'];
$set4 = ['14', '31', '54', '5', '73', '39'];
$set5 = ['10', '3', '22', '59', '73', '39'];
$set6 = ['22', '13', '4', '5', '73', '39']
$set7 = ['40', '3', '22', '5', '13', '30'];
$set8 = ['88', '53', '4', '25', '71', '19'];
$set9 = ['10', '30', '49', '25', '73', '46'];
$set10 = ['10', '3', '4', '5', '73', '11'];

$mergedArray = array_merge($set1, $set2, $set3, $set4, $set5, $set6, $set7, $set8, $set9, $set10);
echo 'Values that appear 1 time: ' . implode(', ', array_keys(getRepeatedNumber($mergedArray, 1))) . '<br>';
echo 'Values that appear 2 times: ' . implode(', ', array_keys(getRepeatedNumber($mergedArray, 2))) . '<br>';
echo 'Values that appear 3 times: ' . implode(', ', array_keys(getRepeatedNumber($mergedArray, 3))) . '<br>';
echo 'Values that appear 4 times: ' . implode(', ', array_keys(getRepeatedNumber($mergedArray, 4))) . '<br>';
echo 'Values that appear 5 times: ' . implode(', ', array_keys(getRepeatedNumber($mergedArray, 5))) . '<br>';
function getRepeatedNumber($mergedArray, $requiredCount)
{
$counts = array_count_values($mergedArray);
$requiredResult = array_filter($counts, function ($value) use ($requiredCount) {
    return $value == $requiredCount;
});
return $requiredResult;}

我的问题: 我想计算频率并回显一个额外的数字数组的结果,这些数字应该 与我的脚本的实际数组集进行比较

例如: 如果我有

$extraset = ['1', '4', '52'] ;

我愿意

但我不明白如何比较 $extraset(我是一个真正的新手..!)

感谢您的帮助!

你可以把这个问题看成一个转换问题。您正在将数字转换为其频率,反之亦然。 array_count_values 将 table 的转化设置为一个数组。数组的键,给出要转换的数字及其值的频率。

所以从构建合并数组开始:

$set1 = ['23', '11', '52', '33', '1', '4'];
$set2 = ['66', '70', '55', '8', '22', '1'];
$set3 = ['38', '21', '52', '51', '53', '9'];
$set4 = ['14', '31', '54', '5', '73', '39'];
$set5 = ['10', '3', '22', '59', '73', '39'];
$set6 = ['22', '13', '4', '5', '73', '39']
$set7 = ['40', '3', '22', '5', '13', '30'];
$set8 = ['88', '53', '4', '25', '71', '19'];
$set9 = ['10', '30', '49', '25', '73', '46'];
$set10 = ['10', '3', '4', '5', '73', '11'];

$mergedArray = array_merge($set1, $set2, $set3, $set4, $set5, $set6, $set7, $set8, $set9, $set10);

接下来设置转换 table:

$frequencies = array_count_values($mergedArray);

然后设置这个table的逆向查找(键是计数,值是数字):

$inverse_frequencies = [];
foreach ($frequencies as $number => $count) {
    $inverse_frequencies[$count][] = $number;
}

对其进行排序,以便键(计数)升序显示:

ksort($inverse_frequencies);

并显示它们:

foreach ($inverse_frequencies as $count => $numbers_array) {
    echo "Values that appear $count times: " . implode(', ', $numbers_array) . '<br>';
}

这个方法比你给的方法更通用也更灵活。

接下来我们取 $extraset 数组并循环查找每个元素的频率并显示它:

$extraset = ['1', '4', '52'] ;

foreach ($extraset as $element) {
    $frequency = $frequencies[$element]??0;
    echo "Extraset element '$element' appears $frequency times<br>";
}

请注意空合并运算符 ?? 的使用,以防我们无法在频率列表中找到数字。

编辑

您可以改进集合的处理方式,方法是将每个新集合推入数组然后合并(理想情况下,您可以将其放入函数中):

$sets = [];
$sets[] = ['23', '11', '52', '33', '1', '4'];
$sets[] = ['66', '70', '55', '8', '22', '1'];
$sets[] = ['38', '21', '52', '51', '53', '9'];
$sets[] = ['14', '31', '54', '5', '73', '39'];
$sets[] = ['10', '3', '22', '59', '73', '39'];
$sets[] = ['22', '13', '4', '5', '73', '39'];
$sets[] = ['40', '3', '22', '5', '13', '30'];
$sets[] = ['88', '53', '4', '25', '71', '19'];
$sets[] = ['10', '30', '49', '25', '73', '46'];
$sets[] = ['10', '3', '4', '5', '73', '11'];

$mergedArray = [];
foreach ($sets as $set) {
    $mergedArray = array_merge($mergedArray, $set);
}

这允许你有一组索引数组($sets),索引越高的数组越新。

然后您可以反向搜索 $sets 数组以查找数字的最新实例:

$reverse_sets = array_reverse($sets);

foreach ($extraset as $element) {
    foreach ($reverse_sets as $index => $set) {
        if (in_array($element, $set)) {
            $actual_index = count($reverse_sets)-$index-1;
            echo "Extraset element '$element' is in set $actual_index<br>";
            break;
        }
    }
}

您需要修复 $index,因为反转数组会重新索引它(不保留键)。

您需要 break 以便在找到一个值后立即转到下一个值。

我会留给你如何处理未找到的元素。