我有一个具有以下值的数组。我正在尝试使用数组 php 数组函数创建一个新数组并尝试最大程度地避免 foreach。我们用于新数组的键是“状态”,根据状态我们为每个邮件 ID 创建新数组。
<?php
[
{
"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"
}
]
?>
结果或新数组如下。我正在尝试使用任何数组函数(如 array_walk_recursive 或 array_reduce)来实现以下结果,这些函数使代码看起来既美观又紧凑。
<?php
[
[
"first_name": "Jon",
"last_name": "Doe",
"email": "jdoe@gmail.com",
"opened": 2,
"blocked": 0,
"hard_bounced": 0,
"soft_bounced": 0,
"received": 0,
"clicked": 0
],
[
"first_name": "Jane",
"last_name": "Doe",
"email": "janedoe@gmail.com",
"opened": 0,
"blocked": 0,
"hard_bounced": 0,
"soft_bounced": 0,
"sent": 2,
"clicked": 0
],
[
"first_name": "Jhon",
"last_name": "Dwaye",
"email": "Jhondw@yahoo.com",
"opened": 0,
"blocked": 0,
"hard_bounced": 0,
"soft_bounced": 0,
"sent": 0,
"clicked": 1
],
]