average and sum from array in php [closed]

99封情书 提交于 2019-12-13 09:54:35

问题


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

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

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

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

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

4) Average activity score based on gender

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

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' => '']
]
];

I tried this

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


回答1:


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

this is working code for this problem

<?
$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>';
?>

hope this works well and good luck




回答2:


As an example, you can manually sort your users into male and female via manual iteration, and then apply your array_sum/count on the 'dob' column...

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

It's upto you to build your sorting/filtration logic, this is a simple exmaple.



来源:https://stackoverflow.com/questions/49841224/average-and-sum-from-array-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!