简体   繁体   English

如何计算和求和数组每一列中的出现次数?

[英]How to count and sum the occurrences in each column of an array?

I want to try to calculate the total occurrences for each "column of digits" in the following list of numbers.我想尝试计算以下数字列表中每个“数字列”的总出现次数。

8353
1619
6889
3850
7596
9594
2557
8838
6224
7075

My expected result for the first digits is:我对第一个数字的预期结果是:

array (
    8 => 2,  // 1st and 8th number starts with 8
    1 => 1,  // 2nd number
    6 => 2,  // 3rd and 9th
    3 => 1,  // 4th
    7 => 2,  // 5th and 10th
    9 => 1,  // 6th
    2 => 1,  // 7th
)

I need the same kind of calculation for the other columns (2nd digits, 3rd digits, 4th digits) all collected into a multi-dimensional array.我需要对所有收集到多维数组中的其他列(第 2 位、第 3 位、第 4 位)进行相同类型的计算。

What I did so far was like this到目前为止我所做的是这样的

while($rowny = mysqli_fetch_assoc($mulai)) {
    $rowData[] = $rowny;
}
for($i=0; $i<100; $i++){
    $hasil[] = $rowData[$i]['result'];

    $pecah = str_split($hasil[$i],1);
    $hitunga = array_count_values($pecah);

    print_r($hitunga);echo "<br/>";
}

So far when I print the $hitunga , it shows the array like this到目前为止,当我打印$hitunga时,它会显示这样的数组

Array ( [8] => 1 [3] => 2 [5] => 1 )
Array ( [1] => 2 [6] => 1 [9] => 1 )
Array ( [6] => 1 [8] => 2 [9] => 1 )
Array ( [3] => 1 [8] => 1 [5] => 1 [0] => 1 )
Array ( [7] => 1 [5] => 1 [9] => 1 [6] => 1 )
Array ( [9] => 2 [5] => 1 [4] => 1 )
Array ( [2] => 1 [5] => 2 [7] => 1 )
Array ( [8] => 3 [3] => 1 )
Array ( [6] => 1 [2] => 2 [4] => 1 )
Array ( [7] => 2 [0] => 1 [5] => 1 )

but this is changing the order of the digits and in some cases there are no numbers in the latter columns.但这改变了数字的顺序,在某些情况下,后面的列中没有数字。 I expect to maintain 4 columns of numbers and get the total occurrences in each column.我希望保留 4 列数字并获得每列中的总出现次数。

  1. Split each number into a 4-element array.将每个数字拆分为一个 4 元素数组。
    ( array_map('str_split', $numbers) ) ( array_map('str_split', $numbers) )
  2. Transpose the new multi-dimensional structure, IOW -- convert columns to rows.转置新的多维结构,IOW——将列转换为行。
    ( array_map(null, ...#1) ) ( array_map(null, ...#1) )
  3. Count the values.计算值。
    ( array_map('array_count_values', #2) ( array_map('array_count_values', #2)

One-liner:单线:

array_map('array_count_values', array_map(null, ...array_map('str_split', $numbers)))

Code: ( Demo )代码:(演示

var_export(
    array_map(
        'array_count_values',
        array_map(
            null,
            ...array_map(
                'str_split',
                $numbers
            )
        )
    )
);

Or if you prefer language constructs, this might be easier to comprehend...或者,如果您更喜欢语言结构,这可能更容易理解......

Code: ( Demo )代码:(演示

$result = [];
foreach ($numbers as $number) {
    foreach (str_split($number) as $index => $digit) {
        $result[$index][$digit] = ($result[$index][$digit] ?? 0) + 1;
    }
}
var_export($result);

Output (from both snippets): Output(来自两个片段):

array (
  0 => 
  array (
    8 => 2,
    1 => 1,
    6 => 2,
    3 => 1,
    7 => 2,
    9 => 1,
    2 => 1,
  ),
  1 => 
  array (
    3 => 1,
    6 => 1,
    8 => 3,
    5 => 3,
    2 => 1,
    0 => 1,
  ),
  2 => 
  array (
    5 => 3,
    1 => 1,
    8 => 1,
    9 => 2,
    3 => 1,
    2 => 1,
    7 => 1,
  ),
  3 => 
  array (
    3 => 1,
    9 => 2,
    0 => 1,
    6 => 1,
    4 => 2,
    7 => 1,
    8 => 1,
    5 => 1,
  ),
)

Significantly less elegant and sophisticated, but this might be logic you're looking for?明显不那么优雅和复杂,但这可能是您正在寻找的逻辑?

<?php
$arr = [
    8353, 1619, 6889,3850, 7596, 9594, 2557, 8838, 6224, 7075,
];

foreach ($arr as $key => $val) {
    $split = str_split($val);
    $count[] = array_count_values($split);
}

foreach ($count as $key => $value) {
    $cleanCount[] = array_values($value);
}

// careful - numbers in table must be equal length
for ($i = 0; $i < strlen($arr[0]); $i++) {
    $result[$i] = array_sum(array_column($cleanCount, $i));
}

echo '<pre>';
print_r($result);
echo '</pre>';

Output (sum of count for each column): Output(每列的计数总和):

Array
(
    [0] => 15
    [1] => 14
    [2] => 9
    [3] => 2
)

working demo工作演示

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

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