gpt4 book ai didi

php - 计算可能性 PHP、MySqli

转载 作者:行者123 更新时间:2023-11-29 12:39:46 25 4
gpt4 key购买 nike

我有一个包含 6 个数字和日期的表格。

1   5   8   14  35  45  Mon
13 26 30 31 43 49 Sat
1 4 13 19 34 39 Wed
2 20 21 24 25 43 Mon
1 6 31 33 39 45 Sat
2 11 16 21 25 28 Wed
17 21 24 37 46 48 Mon
6 10 11 22 28 33 Sat
3 10 23 30 38 45 Wed

我正在尝试创建一个脚本,该脚本将为我提供当天重复次数最多的 6 个数字。

下面的脚本将给出第一列重复的所有数字。

$query = mysqli_query($connect_db,"SELECT first, COUNT(first) AS ct
FROM numbers
GROUP BY first
ORDER BY first DESC");
while($row = mysqli_fetch_array($query)){
echo '<table class="first"><tr><td>'. $row[1].'</td><tr></table>';

}

但我只需要 6 个重复出现次数最高的数字。

数据库: enter image description here

最佳答案

好吧,我想我现在明白了这个问题。试试这个。

$numLabels = array('first', 'second', 'third', 'fourth', 'fifth', 'sixth');

$numbers= array();

$query = "SELECT * FROM numbers";
$rows = mysqli_query($link, $query);

if ($rows) {
while ($row = mysqli_fetch_assoc($rows)) {

//indexing all the numbers and counting occurrences
foreach ($numLabels as $label) {
if (isset($numbers[$row['day']][$row[$label]])) {
$numbers[$row['day']][$row[$label]] ++;
} else {
$numbers[$row['day']][$row[$label]] = 1;
}
}
}

mysqli_free_result($rows);

//sorting by occurrences
foreach (array_keys($numbers) as $key) {
arsort($numbers[$key]);
}

//getting the most common 6
foreach ($numbers as $day => $dayNumbers) {
$numbers[$day] = array_slice(array_keys($dayNumbers), 0, 6);
}
}

//example output format: "Day: num1-num2-num3..."
foreach ($numbers as $day => $dayNumbers) {
echo $day . ': ' . implode('-', $dayNumbers) . '<br />';
}

关于php - 计算可能性 PHP、MySqli,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26281418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com