i have a age data: 18,25,36,20,23,21,31,
Using php how i can show like this data? 10 to 20 years = 1 people, 20 to 30 years = 4 people, 30 to 40 years = 2 people
i have a age data: 18,25,36,20,23,21,31,
Using php how i can show like this data? 10 to 20 years = 1 people, 20 to 30 years = 4 people, 30 to 40 years = 2 people
You could try something like this. It uses array_map
to divide all values in the data by 10 (thus putting them in groups), then array_count_values
to count the number of people in each group.
$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";
}
Output:
0 to 9 years: 0
10 to 19 years: 1
20 to 29 years: 4
30 to 39 years: 2
Update
If you don't want to display groups that have no people in them, use this loop instead:
for ($i = 0; $i <= max(array_keys($groups)); $i++) {
if (isset($groups[$i])) echo $i*10 . " to " . ($i*10+9) . " years: " . $groups[$i] . "\n";
}
Output:
10 to 19 years: 1
20 to 29 years: 4
30 to 39 years: 2
There are many ways but I will prefer directly from mysql query, Something like this
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`
You can refer this answer to calculate age from DOB stored in database
This code collected the birthday dates from your database and groups them for you in your desired ranges.
//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/>';
}
$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) }