php:如何合并和求和相同键值的数组值?

时间:2017-08-13 23:22:42

标签: php

我的代码中有这个原始数组

 [
        {
            "source": {
                "id": 2,
                "name": "Cairo"
            },
            "destination": [
                {
                    "count": 3,
                    "name": "Alex",
                    "price": 2000,
                    "discount": null,
                    "distance": 12,
                    "new": false
                },
                {
                    "count": 4,
                    "name": "Alex",
                    "price": 2000,
                    "discount": null,
                    "distance": 12,
                    "new": false
                }
            ]
        },
        {
            "source": {
                "id": 1,
                "name": "Zagazig"
            },
            "destination": [
                {
                    "count": 1,
                    "name": "Alex",
                    "price": 200,
                    "discount": null,
                    "distance": 100,
                    "new": false
                },
                {
                    "count": 1,
                    "name": "Cairo",
                    "price": 1000,
                    "discount": null,
                    "distance": 100,
                    "new": false
                },
                {
                    "count": 1,
                    "name": "Cairo",
                    "price": 1000,
                    "discount": null,
                    "distance": 100,
                    "new": false
                }
            ]
        }
    ]

在目标数组中,如果在像alex或cairo这样的每个对象中重复名称,我想将它合并到一个对象中,只有一个名为cairo的对象,以及一个名为alex的对象,并计算或者使用计数键

我想要这个结果................ ................

     [
            {
                "source": {
                    "id": 2,
                    "name": "Cairo"
                },
                "destination": [
                    {
                        "count": 7,
                        "name": "Alex",
                        "price": 2000,
                        "discount": null,
                        "distance": 12,
                        "new": false
                    }
                ]
            },
            {
                "source": {
                    "id": 1,
                    "name": "Zagazig"
                },
                "destination": [
                    {
                        "count": 1,
                        "name": "Alex",
                        "price": 200,
                        "discount": null,
                        "distance": 100,
                        "new": false
                    },
  {
                    "count": 2,
                    "name": "Cairo",
                    "price": 1000,
                    "discount": null,
                    "distance": 100,
                    "new": false
                },
                ]
            }
        ]

2 个答案:

答案 0 :(得分:0)

如果您只需要在名称相同时对计数求和,则只需迭代每个数组中的每个目标项,当您找到重复的名称时,只需将计数添加到上一个值即可。它将为您提供一个具有所需输出的新数组:

$a =  [
    array(
        "source" => array(
            "id" => 2,
            "name" => "Cairo"
        ),
        "destination" => [
             array(
                "count" => 3,
                "name" => "Alex",
                "price" => 2000,
                "discount" => null,
                "distance" => 12,
                "new" => false
            ),
             array(
                "count" => 4,
                "name" => "Alex",
                "price" => 2000,
                "discount" => null,
                "distance" => 12,
                "new" => false
            )
        ]
    ),
     array(
        "source" =>  array(
            "id" => 1,
            "name" => "Zagazig"
        ),
        "destination" => [
             array(
                "count" => 1,
                "name" => "Alex",
                "price" => 200,
                "discount" => null,
                "distance" => 100,
                "new" => false
            ),
             array(
                "count" => 1,
                "name" => "Cairo",
                "price" => 1000,
                "discount" => null,
                "distance" => 100,
                "new" => false
            ),
             array(
                "count" => 1,
                "name" => "Cairo",
                "price" => 1000,
                "discount" => null,
                "distance" => 100,
                "new" => false
            )
        ]
    )
];

$new_array = array();
foreach ($a as $e)  {
    $tmp = array();
    foreach ($e['destination'] as $d) {
        if (isset($tmp[$d['name']])) {
            $tmp[$d['name']]['count'] += $d['count'];
        } else {
            $tmp[$d['name']] = $d;
        }
    }
    array_push($new_array,  array(
        'source' => $e['source'],
        'destination' => $tmp
    ));
}
echo json_encode($new_array);

输出结果为:

[
    {
        "source":{
            "id":2,
            "name":"Cairo"
        },
        "destination":{
            "Alex":{
                "count":7,
                "name":"Alex",
                "price":2000,
                "discount":null,
                "distance":12,
                "new":false
            }
        }
    },
    {
        "source":{
            "id":1,
            "name":"Zagazig"
        },
        "destination":{
            "Alex":{
                "count":1,
                "name":"Alex",
                "price":200,
                "discount":null,
                "distance":100,
                "new":false
            },
            "Cairo":{
                "count":2,
                "name":"Cairo",
                "price":1000,
                "discount":null,
                "distance":100,
                "new":false
            }
        }
    }
]

答案 1 :(得分:0)

我建议将任务分成小部分,你可以更好地理解,然后向上移动。

我会帮助你完成内部数组,这是最简单的任务,你会看到一切都会变得更清晰。

所以如果你得到这个json集合:

ReadAsync("qux")

您可以解码:

$json_easy = '[
            {
                "count": 1,
                "name": "Alex",
                "price": 200,
                "discount": null,
                "distance": 100,
                "new": false
            },
            {
                "count": 1,
                "name": "Cairo",
                "price": 1000,
                "discount": null,
                "distance": 100,
                "new": false
            },
            {
                "count": 1,
                "name": "Cairo",
                "price": 1000,
                "discount": null,
                "distance": 100,
                "new": false
            }
        ]';

然后创建一个新数组:

$easy_decode = json_decode($json_easy, true);

如果你仔细观察,你会看到你现在可以在你的php代码中委托你自己的一个函数,并用你喜欢的任何标准对数组中的任何值求和。创建函数后,您可以使用更多for或foreach循环开始在数组中向上移动。