For a few hours, I lost myself in an array.
I have something like this:
$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"
],
];
I need to count (and have the sum) of all the "status" for a specific "Step".
In this case, I wish to have :
$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
],
];
Note: Number of User is not defined (1 to N) and number of Step too (1 to 4).
Try this, see if it works or not.
$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);