php数组问题与分组

时间:2018-04-17 09:08:02

标签: php arrays

我试图从这个阵列中找到男性和女性的平均水平 我发现男性和女性的平均水平,任何人都会帮我在乡下找到它

<?php
$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],
];

foreach($users as $user)
{


       $tmp1[$user['gender']][]=date('Y')-$user['dob'];// calculating age 
       $tmp[$user['country']][] = $user;
}



 $male_avg=array_sum($tmp1['M'])/count($tmp1['M']);

 $female_avg=array_sum($tmp1['F'])/count($tmp1['F']);



?>

5 个答案:

答案 0 :(得分:2)

您可以使用array_reduce汇总国家/地区和性别

$users = ..... //Your array here

//Summarize the data
$summarize = array_reduce($users, function($c,$v){
    $key = $v['country'] . '_' . $v['gender'];
    if ( !isset( $c[ $key ] ) ) $c[ $key ] = array( 'sum' => 0, 'count' => 0 );

    $c[ $key ]['sum'] += ( date('Y') - $v['dob'] );
    $c[ $key ]['count']++;

    return $c;
}, array());

$avegage = array();
foreach( $summarize as $key => $val ) {
    $avegage[ $key ] = $val['sum'] / $val['count'];
}

echo "<pre>";
print_r( $avegage );
echo "</pre>";

这将导致:

Array
(
    [IN_M] => 16.75
    [US_M] => 29
    [UK_F] => 25
    [IN_F] => 21
    [UK_M] => 27
    [JP_F] => 22
    [US_F] => 48
)

使用循环:

$users = .......

//Summarize the data
$summarize = array();
foreach( $users as $user ) {
    $skey = $user['country'] . '_' . $user['gender'];
    if ( !isset( $summarize[ $skey ] ) ) $summarize[ $skey ] = array( 'sum' => 0, 'count' => 0 );

    $summarize[ $skey ]['sum'] += ( date('Y') - $user['dob'] );
    $summarize[ $skey ]['count']++;
}

$avegage = array();
foreach( $summarize as $key => $val ) {
    $avegage[ $key ] = $val['sum'] / $val['count'];
}

答案 1 :(得分:1)

foreach($users as $user)
{

       $tmp[$user['country']][] = date('Y')-$user['dob'];
}

 foreach($tmp as $country=>$country_stat){
    echo $country.' = '.array_sum($country_stat)/count($country_stat).'<br/>';
 }

使用foreach遍历各个国家/地区。

这是国家和性别

foreach($users as $user)
{

       $tmp[$user['country'].'_'.$user['gender']][] = date('Y')-$user['dob'];
}

 foreach($tmp as $country=>$country_stat){
    echo $country.' = '.array_sum($country_stat)/count($country_stat).'<br/>';
 }

答案 2 :(得分:0)

只需为country

添加另一个索引即可

喜欢这个

$tmp1 = [];
foreach($users as $user){
    $tmp1[$user['country']][$user['gender']][]=date('Y')-$user['dob'];
}

$temp2 =[];
foreach ($tmp1 as $key => $gender) {
    foreach ($gender as $key2 => $age) {
        $temp2[$key][$key2]   = array_sum($age)/count($age);
    }
}
echo "<pre>";
print_r($temp2);
echo "</pre>";

仅使用一个foreach

$tmp1 = [];
foreach($users as $user){
    $tmp1[$user['country']][$user['gender']][]=date('Y')-$user['dob'];
    $ele = $tmp1[$user['country']][$user['gender']];
    $tmp2[$user['country']][$user['gender']] = count($ele) > 0 ? array_sum($ele) / count($ele) : $ele;
}

echo "<pre>";
print_r($tmp2);
echo "</pre>";

答案 3 :(得分:0)

您可以使用自己的代码,只需添加给定的行

<?php
    $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],
    ];

    foreach($users as $user)
    {


           $tmp1[$user['gender']][]=date('Y')-$user['dob'];
           $tmp[$user['country']][] = $user;
           // count the gender country wise
           $tmp2[$user['country']][$user['gender']][] = $user['gender'];

    }

    // get the average using foreach loop
    foreach($tmp2 as $key => $value)
    {
        $avg[$key]['M'] = count($value['M'])/(count($value['M']) + count($value['F']));
        $avg[$key]['F'] = count($value['F'])/(count($value['M']) + count($value['F']));
    }


     $male_avg=array_sum($tmp1['M'])/count($tmp1['M']);

     $female_avg=array_sum($tmp1['F'])/count($tmp1['F']);



    ?>

答案 4 :(得分:0)

    foreach($users as $user){
        $tmp1[$user['country']][$user['gender']][]=date('Y')-$user['dob'];
    }


    $output=array();
    foreach ($tmp1 as $key =>$tmp2)
    {if(isset($tmp2['M'])&& isset($tmp2['F'])){
        $male_avg=array_sum($tmp2['M'])/count($tmp2['M']);
        $female_avg=array_sum($tmp2['F'])/count($tmp2['F']);

        $output[]=array('country'=>$key,
            'male'=>$male_avg,
            'female'=>$female_avg);

    }

    }
    print_r($output);


Array
(
    [0] => Array
        (
            [country] => IN
            [male] => 16.75
            [female] => 21
        )

    [1] => Array
        (
            [country] => US
            [male] => 29
            [female] => 48
        )

    [2] => Array
        (
            [country] => UK
            [male] => 27
            [female] => 25
        )

)