对二维数组的分组值进行透视和计数,以创建具有零默认列的新二维数组

问题描述 投票:0回答:4

几个小时,我在阵列中迷失了自己。

我有这样的东西:

$results = [
    "User_1" = [ 
        "Step_1" = "accepted",
        "Step_2" = "accepted",
        "Step_3" = "waiting",
        "Step_4" = "refused"
    ],
    "User_2" = [
        "Step_1" = "waiting",
        "Step_2" = "accepted",
        "Step_3" = "accepted",
        "Step_4" = "refused"
    ],
];

我需要计算(并求和)特定“步骤”的所有“状态”。

在这种情况下,我希望:

$steps = [
    "Step_1" = [
        'acceptedSum' => 1,
        'refusedSum' => 0,
        'waitingSum' => 1
    ],
    "Step_2" =[
        'acceptedSum' => 2,
        'refusedSum' => 0,
        'waitingSum' => 0
    ],
    "Step_3" =[
        'acceptedSum' => 1,
        'refusedSum' => 0,
        'waitingSum' => 1
    ],
    "Step_4" =[
        'acceptedSum' => 0,
        'refusedSum' => 0,
        'waitingSum' => 2
    ],
];

注意:用户数量未定义(1 到 N),步骤数量也未定义(1 到 4)。

php arrays multidimensional-array count grouping
4个回答
2
投票

试试这个,看看是否有效。

$steps = array();
$count = 0;
$keys = array_keys(current($results));
foreach($keys as $key){
$accepted = 0;
$refused = 0;
$waiting = 0;
foreach ($results as $result) {
    foreach ($result as $k => $v) {
        if ($key==$k&&$v == 'accepted') {
            $accepted++;
        }
        if ($key==$k&&$v == 'refused') {
            $refused++;
        }
        if ($key==$k&&$v == 'accepted') {
            $waiting++;
        }

    }
}
$new_array = [
    'acceptedSum' => $accepted,
    'refusedSum' => $refused,
    'withoutAnswerSum' => $waiting
];
$steps[$key] = $new_array;
}

print_r($steps);

2
投票

您可以动态创建现有密钥。您可以将缺失的键添加为 0;

$results = [
    "User_1" => [
        "Step_1" => "accepted",
        "Step_2" => "accepted",
        "Step_3" => "waiting"
    ],
    "User_2" => [
        "Step_1" => "waiting",
        "Step_2" => "accepted",
        "Step_3" => "accepted"
    ],
    "User_3" => [
        "Step_1" => "refused",
        "Step_2" => "refused",
        "Step_3" => "waiting"
    ]
];

$steps = [];
$status = [];

foreach ($results as $result) {
    foreach ($result as $key => $r) {
        if (!array_key_exists($key, $steps)) {
            $steps[$key] = [];
        }
        if (!array_key_exists($r."Sum", $steps[$key])) {
            $steps[$key][$r."Sum"] = 0;
        }
        $steps[$key][$r."Sum"]++;
        $status[] = $r;
    }
}

foreach (array_unique($status) as $au) {
    foreach ($steps as &$step) {
        if (!array_key_exists($au."Sum", $step)) {
            $step[$au."Sum"] = 0;
        }
    }
}

print_r($steps);

Php 输出演示


0
投票

我认为你问题中的输出是错误的,但是你在寻找这样的东西吗

<?php

$results = [
    "User_1" => [
        "Step_1" => "accepted",
        "Step_2" => "accepted",
        "Step_3" => "waiting"
    ],
    "User_2" => [
        "Step_1" => "waiting",
        "Step_2" => "accepted",
        "Step_3" => "accepted"
    ],
    "User_3" => [
        "Step_1" => "refused",
        "Step_2" => "refused",
        "Step_3" => "waiting"
    ]  
];

function sumByStatus($array, $status) {
    $filtered_array = array_filter($array,function($value) use ($status) {
        return $value === $status;
    });

    return count($filtered_array);
}

$newResult = array_map(function($item) {
    return [
        'acceptedSum' => sumByStatus($item, 'accepted'),
        'refusedSumF' => sumByStatus($item, 'refused'),
        'withoutAnswerSum' => sumByStatus($item, 'waiting')
    ];
}, $results);

print_r($newResult);

输出是

Array
(
    [User_1] => Array
        (
            [acceptedSum] => 2
            [refusedSum] => 0
            [withoutAnswerSum] => 1
        )

    [User_2] => Array
        (
            [acceptedSum] => 2
            [refusedSum] => 0
            [withoutAnswerSum] => 1
        )

    [User_3] => Array
        (
            [acceptedSum] => 0
            [refusedSum] => 2
            [withoutAnswerSum] => 1
        )

)

0
投票

由于开发人员合理地知道自定义组名称及其所需的顺序,因此您可以对默认的二级键进行硬编码,每组中包含 0 个值。

仅当第一次遇到某个步骤时,才声明该组的默认值。 每次迭代时,将指定的深度关联值加一。

代码:演示

$defaults = array_fill_keys(['acceptedSum', 'refusedSum', 'waitingSum'], 0);
$result = [];
foreach ($results as $row) {
    foreach ($row as $k => $v) {
        $result[$k] ??= $defaults;
        ++$result[$k][$v . 'Sum'];
    }
}
var_export($result);
© www.soinside.com 2019 - 2024. All rights reserved.