平均值和求和从PHP数组 [英] average and sum from array in php

查看:110
本文介绍了平均值和求和从PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为帮助重新定位营销活动,我们需要创建一个用户人口统计报告

To help re-target a marketing campaign, we need to create a user demography report

我想从Give数组中找到以下内容以帮助我

I want to find the following from the give array pls help me

1)所有男性和女性用户的平均年龄

1) The average age of all male, and female users

2)每个国家/地区的男性和女性使用者的平均年龄

2) The average age of male, and female users in each country

3)由于我们不针对儿童,因此不应该包括16岁以下的用户

3) As we are not targeting children, users under 16 should not be included

4)基于性别的平均活动得分

4) Average activity score based on gender

5)16-20岁,20岁以上年龄段的总活动得分

5) Sum activity score by age group 16-20, 20+

6)活动得分最高和最低的国家

6) Countries with highest and lowest activity score

输入和输出数组如下所示.请使用正确的值更新输出数组

Input and output arrays are given below. Please update output array with correct values

$users = [
['id' => 1, 'gender' => 'M', 'dob' => 1990, 'country' => 'IN', 'activity_score' => 34],
['id' => 2, 'gender' => 'M', 'dob' => 1980, 'country' => 'US', 'activity_score' => 9],
['id' => 3, 'gender' => 'F', 'dob' => 1993, 'country' => 'UK', 'activity_score' => 45],
['id' => 4, 'gender' => 'M', 'dob' => 1998, 'country' => 'IN', 'activity_score' => 0],
['id' => 5, 'gender' => 'F', 'dob' => 1997, 'country' => 'IN', 'activity_score' => 234],
['id' => 6, 'gender' => 'M', 'dob' => 1991, 'country' => 'UK', 'activity_score' => -6],
['id' => 7, 'gender' => 'F', 'dob' => 1992, 'country' => 'JP', 'activity_score' => 9],
['id' => 8, 'gender' => 'M', 'dob' => 1998, 'country' => 'US', 'activity_score' => 45],
['id' => 9, 'gender' => 'F', 'dob' => 2000, 'country' => 'JP', 'activity_score' => 5],
['id' => 10, 'gender' => 'M', 'dob' => 2006, 'country' => 'IN', 'activity_score' => 7],
['id' => 11, 'gender' => 'F', 'dob' => 1970, 'country' => 'US', 'activity_score' => 32],
['id' => 12, 'gender' => 'M', 'dob' => 2011, 'country' => 'IN', 'activity_score' => 21],
];

$output = [
'avg_age' => ['M' => 0,'F' => 0],
'avg_age_by_country' => [
'IN' => ['M' => 0,'F' => 0],
'US' => ['M' => 0,'F' => 0],
'UK' => ['M' => 0,'F' => 0],
'JP' => ['M' => 0,'F' => 0],
],
'activity_score' => [
'avg_score_by_gender' => ['M' => 0,'F' => 0],
'sum_score_by_age_group' => ['16-20' => 0, '20+' => 0],
'country' => ['first' => '', 'last' => '']
]
];

我尝试过

foreach($users as $user) {
    if($user['gender'=='M']) {
        $avg['M']=$user;
    }
    else if($user['gender'=='F']) {
        $avg['M']=$user;
    }
}

print_r($avg);

我尝试过这个,请帮我解决这个问题

I tried this please help me to solve this

推荐答案

要计算平均值,您必须更新输出数组以包含一些重要数据,例如用户数M,F 总年龄M,f 总分M,F 以及每个国家/地区的用户数和总年龄 然后您可以通过将总数除以数字来计算平均值 男性平均年龄=男性总年龄/男性数量

to calculate the average you must update your output array to includes some important data like number of users M,F total age M,f total score M,F and number of users and total age for each country and then you can calculate the average by dividing the total by the number average male age = total age males / number of males

这是此问题的有效代码

<?
$output = array(
'number_of_users' => array('M' => 0,'F' => 0),//New array to hold number of users
'total_users_age' => array('M' => 0,'F' => 0),//New array to hold Total Ages
'total_users_score' => array('M' => 0,'F' => 0),//new array to hold total users score
'avg_age' => array('M' => 0,'F' => 0),
//update each country array to add 
//NM - number of males
//NF - number of females
//AM - Total males age
//AF - Total females age
'avg_age_by_country' => array(
'IN' => array('M' => 0,'F' => 0,'NM' => 0,'NF' => 0,'AM' => 0,'AF' => 0),
'US' => array('M' => 0,'F' => 0,'NM' => 0,'NF' => 0,'AM' => 0,'AF' => 0),
'UK' => array('M' => 0,'F' => 0,'NM' => 0,'NF' => 0,'AM' => 0,'AF' => 0),
'JP' => array('M' => 0,'F' => 0,'NM' => 0,'NF' => 0,'AM' => 0,'AF' => 0),
),
'activity_score' => array(
'avg_score_by_gender' => array('M' => 0,'F' => 0),
'sum_score_by_age_group' => array('16-20' => 0, '20+' => 0),
'country' => array('first' => '', 'last' => '')
)
);


$max = 0;//variable to detect the highest county score
$min = "notSet";//variable to detect the lowest county score
foreach($users as $user){
$output['number_of_users'][$user['gender']]++;//increase number of users based on user gender
$output['total_users_age'][$user['gender']] = ($output['total_users_age'][$user['gender']] + (date(Y) - $user['dob']));//add user age to total ages based on his gender
$output['total_users_score'][$user['gender']] = ($output['total_users_score'][$user['gender']] + (date(Y) - $user['activity_score']));//add user score to total score based on his gender
$output['avg_age_by_country'][$user['country']]['N'.$user['gender']]++;//increase number of users based on user gender and country
$output['avg_age_by_country'][$user['country']]['A'.$user['gender']] = ($output['avg_age_by_country'][$user['country']]['A'.$user['gender']] + (date(Y) - $user['dob']));//add user age to total ages based on his gender and country
//detect user age group and add up user score
if((date(Y) - $user['dob']) >= 16 && (date(Y) - $user['dob']) <= 20){
$output['activity_score']['sum_score_by_age_group']['16-20'] = ($output['activity_score']['sum_score_by_age_group']['16-20'] + $user['activity_score']);
}elseif((date(Y) - $user['dob']) > 20){
$output['activity_score']['sum_score_by_age_group']['20+'] = ($output['activity_score']['sum_score_by_age_group']['16-20'] + $user['activity_score']);
}
//detect max country
if($user['activity_score'] > $max){
$output['activity_score']['country']['first'] = $user['country'];
$max = $user['activity_score'];
}
//detect min country 
//notSet mean this is the first user
if($user['activity_score'] < $min || $min == "notSet"){
$output['activity_score']['country']['last'] = $user['country'];
$min = $user['activity_score'];
}
}
/******* calculating averages******/
$output['avg_age']['M'] = $output['total_users_age']['M'] / $output['number_of_users']['M'];
$output['avg_age']['F'] = $output['total_users_age']['F'] / $output['number_of_users']['F'];
$output['activity_score']['avg_score_by_gender']['M'] = $output['total_users_score']['M'] / $output['number_of_users']['M'];
$output['activity_score']['avg_score_by_gender']['F'] = $output['total_users_score']['F'] / $output['number_of_users']['F'];
/******* calculating averages for each country******/
foreach($output['avg_age_by_country'] as $key => $country ){
$output['avg_age_by_country'][$key]['M'] = $country['AM'] / $country['NM'];
$output['avg_age_by_country'][$key]['F'] = $country['AF'] / $country['NF'];
}

echo '<pre>';
print_r($output);
echo '</pre>';
?>

希望这很好 祝你好运

这篇关于平均值和求和从PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆