php中数组的平均值和总和

时间:2018-04-15 11:06:40

标签: php arrays string average

为了帮助重新定位营销广告系列,我们需要创建用户人口统计报告

我想从给定数组中找到以下内容请帮助我

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

2)每个国家的男性和女性用户的平均年龄

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

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

5)按年龄组16-20,20 +总和活动得分

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

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

$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);

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

2 个答案:

答案 0 :(得分:1)

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

这是此问题的工作代码

<?
$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>';
?>
希望这很好用 祝你好运

答案 1 :(得分:0)

例如,您可以通过手动迭代手动将用户分为男性和女性,然后在“dob”列上应用array_sum / count ...

// Sort $users into $male or $female
$male = array();
$female = array();
foreach( $users as $user )
{
  if( $user['gender'] == 'M' )
  {
    $male[] = $user;
  }
  elseif( $user['gender'] == 'F' )
  {
    $female[] = $user;
  }
}

// Average DOB value
vardump( array_sum( array_column( $male, 'dob' ) ) / count( $male ) );
vardump( array_sum( array_column( $female, 'dob' ) ) / count( $female ) );

由您来构建排序/过滤逻辑,这是一个简单的例子。