<?php
$source = [
[
"mail_id" => "29848947",
"last_name" => "Doe",
"first_name" => "Jon",
"email" => "jdoe@gmail.com",
"status" => "opened"
],
[
"mail_id" => "340980398",
"last_name" => "Doe",
"first_name" => "Jane",
"email" => "janedoe@gmail.com",
"status" => "sent"
],
[
"mail_id" => "877586",
"last_name" => "Dwaye",
"first_name" => "Jhon",
"email" => "Jhondw@yahoo.com",
"status" => "clicked"
],
[
"mail_id" => "225253463",
"last_name" => "Doe",
"first_name" => "Jon",
"email" => "jdoe@gmail.com",
"status" => "opened"
],
[
"mail_id" => "849849w4",
"last_name" => "Doe",
"first_name" => "Jane",
"email" => "janedoe@gmail.com",
"status" => "sent"
]
];
$result = array_values(array_reduce($source, function($carry, $event) {
if(!array_key_exists($event['email'], $carry)) {
$carry[$event['email']] = [
"first_name" => $event["first_name"],
"last_name" => $event["last_name"],
"email" => $event["email"],
"opened" => 0,
"blocked" => 0,
"hard_bounced" => 0,
"sent" => 0,
"clicked" => 0
];
}
$carry[$event['email']][$event["status"]]++;
return $carry;
}, []));
print_r($result);
- Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- Array
(
[0] => Array
(
[first_name] => Jon
[last_name] => Doe
[email] => jdoe@gmail.com
[opened] => 2
[blocked] => 0
[hard_bounced] => 0
[sent] => 0
[clicked] => 0
)
[1] => Array
(
[first_name] => Jane
[last_name] => Doe
[email] => janedoe@gmail.com
[opened] => 0
[blocked] => 0
[hard_bounced] => 0
[sent] => 2
[clicked] => 0
)
[2] => Array
(
[first_name] => Jhon
[last_name] => Dwaye
[email] => Jhondw@yahoo.com
[opened] => 0
[blocked] => 0
[hard_bounced] => 0
[sent] => 0
[clicked] => 1
)
)
preferences:
164.58 ms | 410 KiB | 5 Q