如何正确面对数组中的值并回显结果?
How to properly confront values from array and echo the result?
我有一个包含 10 组数值的脚本。每组有六个不同的数值,从 1 到 99。
<?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'];
我如何告诉脚本
- 面对所有集合中的每个值
- 回显集合中出现的数字 1 次、2 次和 3 次?
我正在尝试使用 array_diff,但我不知道如何让它工作(我是 php 的新手)
感谢您的帮助和知识!
可能是最难的方法,但希望你能得到这份工作!
http://sandbox.onlinephpfunctions.com/code/b814925df9d280a3715100eb2210b2c2fd4f5af3
<?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'];
$next = 1;
$var = "set{$next}";
$count = [];
while (isset($$var)) {
$set = $$var;
foreach ($set as $val) {
if (!isset($count[$val])) {
$count[$val] = 0;
}
$count[$val]++;
}
$next++;
$var = "set{$next}";
}
$result = [
1 => 0,
2 => 0,
3 => 0
];
foreach ($count as $value => $occurrences) {
if ($occurrences === 0 || $occurrences > 3) {
continue;
}
$result[$occurrences]++;
}
print_r($result);
Array
(
[1] => 20
[2] => 7
[3] => 3
)
您可以将这些数组合并为一个 array_merge()
. This makes it easy to get the counts with array_count_values()
。
一旦你有了计数,你就可以使用 array_filter()
来获得你想要的结果。
这是工作示例http://sandbox.onlinephpfunctions.com/code/95cbae41e146b380d5ba405cdf4bfea4e018f07a
<?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);
$counts = array_count_values($mergedArray);
$requiredCount = 3; ## Change this value to whatever counts you need
$requiredResult = array_filter($counts, function ($value) use ($requiredCount){
return $value == $requiredCount;
});
var_dump($requiredResult);
## Or you can echo the keys like this
echo implode(', ', array_keys($requiredResult));
您可以将代码包装在一个函数中,以获取在集合中出现 X 次的数字。这是工作示例 http://sandbox.onlinephpfunctions.com/code/af414606a5e881b11638076e89342183182b8b80
的 link
<?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>';
function getRepeatedNumber($mergedArray, $requiredCount)
{
$counts = array_count_values($mergedArray);
$requiredResult = array_filter($counts, function ($value) use ($requiredCount) {
return $value == $requiredCount;
});
return $requiredResult;
}
假设有一个非特定数量的数组要计算,但它们每个都遵循相同的命名约定,您可以使用可变变量:
<?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'];
$i = 1;
$data = array();
while(${"set" . $i} !== null) {
foreach(${"set" . $i} as $number) {
if(isset($data[$number])) {
$data[$number] += 1;
} else {
$data[$number] = 1;
}
}
$i++;
}
foreach($data as $number => $count) {
if($count < 4) {
echo "$number appears $count times in all of the arrays \n";
}
}
?>
我有一个包含 10 组数值的脚本。每组有六个不同的数值,从 1 到 99。
<?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'];
我如何告诉脚本
- 面对所有集合中的每个值
- 回显集合中出现的数字 1 次、2 次和 3 次?
我正在尝试使用 array_diff,但我不知道如何让它工作(我是 php 的新手)
感谢您的帮助和知识!
可能是最难的方法,但希望你能得到这份工作!
http://sandbox.onlinephpfunctions.com/code/b814925df9d280a3715100eb2210b2c2fd4f5af3
<?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'];
$next = 1;
$var = "set{$next}";
$count = [];
while (isset($$var)) {
$set = $$var;
foreach ($set as $val) {
if (!isset($count[$val])) {
$count[$val] = 0;
}
$count[$val]++;
}
$next++;
$var = "set{$next}";
}
$result = [
1 => 0,
2 => 0,
3 => 0
];
foreach ($count as $value => $occurrences) {
if ($occurrences === 0 || $occurrences > 3) {
continue;
}
$result[$occurrences]++;
}
print_r($result);
Array
(
[1] => 20
[2] => 7
[3] => 3
)
您可以将这些数组合并为一个 array_merge()
. This makes it easy to get the counts with array_count_values()
。
一旦你有了计数,你就可以使用 array_filter()
来获得你想要的结果。
这是工作示例http://sandbox.onlinephpfunctions.com/code/95cbae41e146b380d5ba405cdf4bfea4e018f07a
<?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);
$counts = array_count_values($mergedArray);
$requiredCount = 3; ## Change this value to whatever counts you need
$requiredResult = array_filter($counts, function ($value) use ($requiredCount){
return $value == $requiredCount;
});
var_dump($requiredResult);
## Or you can echo the keys like this
echo implode(', ', array_keys($requiredResult));
您可以将代码包装在一个函数中,以获取在集合中出现 X 次的数字。这是工作示例 http://sandbox.onlinephpfunctions.com/code/af414606a5e881b11638076e89342183182b8b80
的 link<?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>';
function getRepeatedNumber($mergedArray, $requiredCount)
{
$counts = array_count_values($mergedArray);
$requiredResult = array_filter($counts, function ($value) use ($requiredCount) {
return $value == $requiredCount;
});
return $requiredResult;
}
假设有一个非特定数量的数组要计算,但它们每个都遵循相同的命名约定,您可以使用可变变量:
<?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'];
$i = 1;
$data = array();
while(${"set" . $i} !== null) {
foreach(${"set" . $i} as $number) {
if(isset($data[$number])) {
$data[$number] += 1;
} else {
$data[$number] = 1;
}
}
$i++;
}
foreach($data as $number => $count) {
if($count < 4) {
echo "$number appears $count times in all of the arrays \n";
}
}
?>