【问题标题】:How to show total no. of age group如何显示总数年龄组
【发布时间】:2019-05-03 06:58:00
【问题描述】:

我有一个年龄数据: 18,25,36,20,23,21,31,

使用 php 我如何显示这样的数据? 10到20年=1人,20到30年=4人,30到40年=2人

【问题讨论】:

  • 你的数据是数组吗?
  • 请显示您当前的代码和您的尝试。
  • 实际上在我的数据库中我有用户出生日期,例如16-12-1997,现在我需要显示年龄组
  • 不在数组中#Jonathan K,谢谢 :)
  • 这些答案之一是否解决了您的问题?如果没有,您能否提供更多信息来帮助回答?否则,请考虑将最能解决您的问题的答案标记为已接受。见stackoverflow.com/help/someone-answers

标签: php


【解决方案1】:

你可以试试这样的。它使用array_map 将数据中的所有值除以 10(从而将它们分组),然后使用array_count_values 计算每个组中的人数。

$ages = [18,25,36,20,23,21,31];
$groups = array_count_values(array_map(function ($v) { return (int)($v / 10); }, $ages));
for ($i = 0; $i <= max(array_keys($groups)); $i++) {
    echo $i*10 . " to " . ($i*10+9) . " years: " . (isset($groups[$i]) ? $groups[$i]: 0) . "\n";
}

输出:

0 to 9 years: 0 
10 to 19 years: 1 
20 to 29 years: 4 
30 to 39 years: 2

更新

如果您不想显示其中没有人的组,请改用此循环:

for ($i = 0; $i <= max(array_keys($groups)); $i++) {
    if (isset($groups[$i])) echo $i*10 . " to " . ($i*10+9) . " years: " . $groups[$i] . "\n";
}

输出:

10 to 19 years: 1 
20 to 29 years: 4 
30 to 39 years: 2

Demo on 3v4l.org

【讨论】:

  • 我的年龄没有括号,那个括号怎么来的?
  • @Ashish 如果您指的是0 to 9 years 组,我不确定您是否想要空组。如果不这样做,您可以改用第二个循环代码。
【解决方案2】:

此代码从您的数据库中收集生日日期,并将它们分组到您想要的范围内。

//Please update this section to grant access to your database
$username = 'root'; //Your Database Username
$password = ''; //Your Database Password
$database = 'test_sample'; //Your Database Name
$table_name = 'ages'; //Your Table Name
$column_name = 'date_of_birth'; // The Column where the birthday dates can be accessed

$query = 'SELECT `'.$column_name.'` FROM `'.$table_name.'`';
$db_connection = mysqli_connect('localhost', $username, $password, $database);

$result = mysqli_query($db_connection, $query);

$current_ages = [];
$now = new DateTime();
while($data = mysqli_fetch_array($result)) {
    $date = new DateTime($data[$column_name]);
    $interval = $now->diff($date);
    $current_ages[] = $interval->y;
}

// $current_ages = [18,25,36,20,23,21,31];

$desired_range = [
   [10,20], [21,30], [31,40], [41,50]
];

$result = [];
foreach($current_ages as $data) {
  foreach($desired_range as $index => $range) {
    if($data >= $range[0] && $data <= $range[1]) {
       (isset($result[$index]))? $result[$index]++ : $result[$index] = 1;
       break;
    }
  }
}

foreach($desired_range as $index => $range) {
    $count = (isset($result[$index]))? $result[$index] : 0;
    echo $range[0]." to ".$range[1]." years = ".$count.' <br/>';
}

【讨论】:

  • 我更新了代码,直接从您的数据库中收集生日日期并为您分组。只需编辑前 5 行中的数据库变量即可使其与您的应用程序数据库一起使用
【解决方案3】:
$ages = [18,25,36,20,23,21,31];
$years = array(); // floor range
foreach ($ages as $age)
{
    $m = (int)($age / 10);
    (isset($years[($m*10)])) ? $years[($m*10)]++ : $years[($m*10)] = 1;
}
var_dump($years); // output: array(3) { [10]=> int(1) [20]=> int(4) [30]=> int(2) } 

【讨论】:

    【解决方案4】:

    有很多方法,但我更喜欢直接从 mysql 查询,像这样

    SELECT IF (`age` < 10, '0 to 10 years',
              IF(`age` > 10 AND `age` < 20, '10 to 20 years', 
                IF(`age` > 20 AND `age` < 30, '20 to 30 years', 'more than 30 years')
              )
            ) AS age_group,
            count(*) as `counts`
      FROM `table_name`
      Group by `age_group`
    

    您可以参考这个answer 来从数据库中存储的 DOB 计算年龄

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 2021-01-09
      • 2015-05-22
      • 2016-04-28
      • 1970-01-01
      相关资源
      最近更新 更多