将PHP数组转换为指定的json结构

时间:2018-08-12 10:42:58

标签: php json foreach

我需要获取如下的JSON结构:

{
   "emails": [
        {
            "sender": "shihas@abc.com",
            "unread_count": 2,
            "items": [
                {
                    "id": "89",
                    "email": "shihas@abc.com",
                    "read": "0",
                },
                {
                    "id": "32",
                    "email": "shihas@abc.com",
                    "read": "0",
                }
            ]
       },
       {
            "sender": "marias123@gmail.com",
            "unread_count": 0,
            "items": [
                {
                    "id": "2",
                    "email": "marias123@gmail.com",
                    "read": "1",
                }
            ]
       },
       {
            "sender": "gutar4320@hotmail.com",
            "unread_count": 1,
            "items": [
                {
                    "id": "1",
                    "email": "gutar4320@hotmail.com",
                    "read": "0",
                }
            ]
       }
   ]
}  

数组($ hire_email):

在下面的数组中,我需要根据email对所有详细信息进行分组。还要算数未读邮件的数量(即已读= 0)。

Array
(
[0] => Array
    (
        [id] => 89
        [email] => shihas@abc.com
        [read] => 0
    )

[1] => Array
    (
        [id] => 32
        [email] => shihas@abc.com
        [read] => 0
    )

[2] => Array
    (
        [id] => 2
        [email] => marias123@gmail.com
        [read] => 1
    )

[3] => Array
    (
        [id] => 1
        [email] => gutar4320@hotmail.com
        [read] => 0
    )
)

以下是用于维护JSON结构的代码段。

foreach($hire_email as $val) {
    if($val['read']==0){ $count++; }else{ $count = 0;}
    $hire_group_email[$val['email']]['sender'] = $val['email'];
    $hire_group_email[$val['email']]['unread_count'] = $count;
    $hire_group_email[$val['email']]['items'][] = $val;
}
$output["emails"][] = $hire_group_email;
echo json_encode($output);

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题。

$hire_email =array(
    array(
        "id" => "89",
        "email" => "shihas@abc.com",
        "read" => "0"
    ),

    array
        (
            "id" => "32",
            "email" => "shihas@abc.com",
            "read" => "0"
        ),

    array
        (
            "id" => "2",
            "email" => "marias123@gmail.com",
            "read" => "1"
        ),

    array
        (
            "id" => "1",
            "email" => "gutar4320@hotmail.com",
            "read" => "0"
        )
);
$tmp = array();

foreach($hire_email as $arg)
{
    $tmp[$arg['email']][] = $arg;
}

$output = array();

foreach($tmp as $type => $labels)
{
    $count = 0;
    foreach ($labels as $value) {
        if($value['read']==0){ $count++; }else{ $count = 0;}
    }
    $output[] = array(
        'sender' => $type,
        'unread_count' => $count,
        'items' => $labels
    );
}

echo json_encode($output);

答案 1 :(得分:0)

尝试使用array_sumarray_column来做到这一点,其余的只是挑选出第一项值。

$array = json_decode($json, true)['emails'];

$result = [];
foreach($array as $val) {
    $result[] = [
        'id' => $val['items'][0]['id'],
        'email' => $val['items'][0]['email'],
        'read' => array_sum(array_column($val['items'], 'read'))
    ];
}

$output["emails"] = $result;

echo json_encode($output, JSON_PRETTY_PRINT);

结果:

{
    "emails": [
        {
            "id": "89",
            "email": "shihas@abc.com",
            "read": 0
        },
        {
            "id": "2",
            "email": "marias123@gmail.com",
            "read": 1
        },
        {
            "id": "1",
            "email": "gutar4320@hotmail.com",
            "read": 0
        }
    ]
}

https://3v4l.org/hi7qm

如果您想要显示它,如; p(我的错,请误读):

遍历每个项目,然后遍历items并使用它来构建输出。

$array = json_decode($json, true)['emails'];

$result = [];
foreach($array as $val) {
    foreach ($val['items'] as $item) {
        $result[] = [
            'id' => $item['id'],
            'email' => $item['email'],
            'read' => array_sum(array_column($val['items'], 'read'))
        ];
    }
}

$output["emails"] = $result;

echo json_encode($output, JSON_PRETTY_PRINT);

结果:

{
    "emails": [
        {
            "id": "89",
            "email": "shihas@abc.com",
            "read": 0
        },
        {
            "id": "32",
            "email": "shihas@abc.com",
            "read": 0
        },
        {
            "id": "2",
            "email": "marias123@gmail.com",
            "read": 1
        },
        {
            "id": "1",
            "email": "gutar4320@hotmail.com",
            "read": 0
        }
    ]
}

https://3v4l.org/eU95A