简体   繁体   English

我如何使用php获取每个年龄段的人数

[英]how can i get count of each age group using php

please check this : 请检查此:

Array
(
    [0] => 46-65
    [1] => 7-12|31-45
    [2] => 31-45
    [3] => 31-45
    [4] => 66+
    [5] => 18-30
    [6] => 46-65
    [7] => 13-17|46-65

Here i converted string to array then i got below array : 在这里,我将字符串转换为数组,然后在数组下面:

Array
(
    [0] => Array
        (
            [0] => 46-65
        )

    [1] => Array
        (
            [0] => 7-12
            [1] => 31-45
        )

    [2] => Array
        (
            [0] => 31-45
        )

    [3] => Array
        (
            [0] => 31-45
        )

    [4] => Array
        (
            [0] => 66+
        )

    [5] => Array
        (
            [0] => 18-30
        )

    [6] => Array
        (
            [0] => 46-65
        )

    [7] => Array
        (
            [0] => 13-17
            [1] => 46-65
        )

    [8] => Array
        (
            [0] => 31-45
        )

    [9] => Array
        (
            [0] => 31-45
        )

    [10] => Array
        (
            [0] => 31-45
        )

    [11] => Array
        (
            [0] => 18-30
        )

    [12] => Array
        (
            [0] => 
        )

    [13] => Array
        (
            [0] => 46-65
        )

    [14] => Array
        (
            [0] => 7-12
            [1] => 31-45
        )

    [15] => Array
        (
            [0] => 18-30
        )

    [16] => Array
        (
            [0] => 18-30
            [1] => 31-45
        )

    [17] => Array
        (
            [0] => 18-30
        )

    [18] => Array
        (
            [0] => 31-45
        )

    [19] => Array
        (
            [0] => 18-30
        )

    [20] => Array
        (
            [0] => 13-17
            [1] => 18-30
            [2] => 46-65
        )
}

I stored age group in string format because each row can be have multiple age group. 我将年龄组存储为字符串格式,因为每一行可以有多个年龄组。 after that i converted into array. 之后,我转换为数组。 How can i get count of each age group like 46-65 is 6 times 18-30 is 8 times. 我怎样才能得到像46-65岁这样的每个年龄段的人数的6倍18-30是8倍。 HEre i want count of each age group 我在这里想要每个年龄段的人数

This code should work for that: 该代码应适用于此:

<?php
$selections = [/** your array here */];
$counted = [];

// Loop full list
foreach($selections as $selection) {

    // Loop age elements in list
    foreach ($selection as $age) {

        // If not already counted initialise age range
        if(!isset($counted[$age])) {
            $counted[$age] = 0;
        }

        // Increment age range
        $counted[$age]++;
    }
}

var_dump($counted);

I'm not sure I fully understood tom problem. 我不确定我是否完全理解汤姆的问题。 Try something like this. 尝试这样的事情。 This will return you a array with as key the groups and as value the number of times it has been met. 这将返回一个数组,该数组具有作为组的键和作为满足条件的次数的值。 I used your second array or you already subdivided into sub array 我使用了第二个数组,或者您已经细分为子数组

$groups = [];
foreach($array as $subArray) {
  foreach ($subArray as $group) {
    if (isset($groups[$group])) {
      $groups[$group] += 1;
    } else {
      $groups[$group] = 1;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM