合并 php 数组中的相同值

combine identical values in php arrays

我有一个很难破解的。

我有这个包含电子邮件的数组:

Array ( 
[0] => 9000@domain.com 
[1] => hasseris@domain.com 
[2] => storcenter@domain.com 
[3] => osteraa@domain.com 
[4] => vejgaard@domain.com 
[5] => gistrup@domain.com 
[6] => storvorde@domain.com 
)

这个数组的表单 ID:

Array ( 
[0] => 1a21f6b7-8025-4724-b983-14745823ede1 
[1] => 4c8a63c0-4650-4884-b83c-70054f538ec2 
[2] => 148c1886-6674-45f3-9665-748bb6a34f4c 
[3] => 02fc3795-9457-4b02-91da-7d4b63eac1c0 
)

通过这个 foreach,我 运行 通过数组中的每封邮件创建一个完整的 JSON 对象

foreach($mails_array as $key => $value) {

    foreach($formIds as $form) {

         $branchArray = array(
                   "FormIds" => [''.$form.''],
                   "Conditions" => array("sendto" => $value),
                   "From" => $date_from,
                   "To" => $date_to,
                   "Days" => ''
               );

         array_push($array, $branchArray);
    }  
}

$json = json_encode($array);
$string = preg_replace('/"([^"]+)"\s*:\s*/', ':', $json);

这将创建此 json,用于根据条件 (sendto) 在 X 表单上调用 API 至 return 次提交:

[{FormIds:["1a21f6b7-8025-4724-b983-14745823ede1"],Conditions:{sendto:"9000@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["4c8a63c0-4650-4884-b83c-70054f538ec2"],Conditions:{sendto:"9000@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["148c1886-6674-45f3-9665-748bb6a34f4c"],Conditions:{sendto:"9000@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["02fc3795-9457-4b02-91da-7d4b63eac1c0"],Conditions:{sendto:"9000@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["1a21f6b7-8025-4724-b983-14745823ede1"],Conditions:{sendto:"hasseris@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["4c8a63c0-4650-4884-b83c-70054f538ec2"],Conditions:{sendto:"hasseris@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["148c1886-6674-45f3-9665-748bb6a34f4c"],Conditions:{sendto:"hasseris@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["02fc3795-9457-4b02-91da-7d4b63eac1c0"],Conditions:{sendto:"hasseris@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["1a21f6b7-8025-4724-b983-14745823ede1"],Conditions:{sendto:"storcenter@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["4c8a63c0-4650-4884-b83c-70054f538ec2"],Conditions:{sendto:"storcenter@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["148c1886-6674-45f3-9665-748bb6a34f4c"],Conditions:{sendto:"storcenter@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["02fc3795-9457-4b02-91da-7d4b63eac1c0"],Conditions:{sendto:"storcenter@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["1a21f6b7-8025-4724-b983-14745823ede1"],Conditions:{sendto:"osteraa@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["4c8a63c0-4650-4884-b83c-70054f538ec2"],Conditions:{sendto:"osteraa@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["148c1886-6674-45f3-9665-748bb6a34f4c"],Conditions:{sendto:"osteraa@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["02fc3795-9457-4b02-91da-7d4b63eac1c0"],Conditions:{sendto:"osteraa@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["1a21f6b7-8025-4724-b983-14745823ede1"],Conditions:{sendto:"vejgaard@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["4c8a63c0-4650-4884-b83c-70054f538ec2"],Conditions:{sendto:"vejgaard@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["148c1886-6674-45f3-9665-748bb6a34f4c"],Conditions:{sendto:"vejgaard@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["02fc3795-9457-4b02-91da-7d4b63eac1c0"],Conditions:{sendto:"vejgaard@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["1a21f6b7-8025-4724-b983-14745823ede1"],Conditions:{sendto:"gistrup@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["4c8a63c0-4650-4884-b83c-70054f538ec2"],Conditions:{sendto:"gistrup@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["148c1886-6674-45f3-9665-748bb6a34f4c"],Conditions:{sendto:"gistrup@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["02fc3795-9457-4b02-91da-7d4b63eac1c0"],Conditions:{sendto:"gistrup@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["1a21f6b7-8025-4724-b983-14745823ede1"],Conditions:{sendto:"storvorde@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["4c8a63c0-4650-4884-b83c-70054f538ec2"],Conditions:{sendto:"storvorde@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["148c1886-6674-45f3-9665-748bb6a34f4c"],Conditions:{sendto:"storvorde@domain.com"},From:"2016-03-23",To:"today",Days:""},{FormIds:["02fc3795-9457-4b02-91da-7d4b63eac1c0"],Conditions:{sendto:"storvorde@domain.com"},From:"2016-03-23",To:"today",Days:""}]

其中 FormIds 是提交的特定表单的 GUID。 "sendto" 条件中的邮件是我希望在特定表单上提交的所有邮件。

举个例子,我试图让所有 4 个表单的所有提交都发送到 9000@domain.com

上面的jsonreturn是这样的,这里的数字是投稿条件:

{
  "arr": [
    1,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    1,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0
  ]
}

所以我知道 returned JSON 中的前 4 个项目(4 个表单 ID)是 9000@domain.com (1,0,0,0) , 接下来的 4 为 hasseris@domain.com (0,0,0,0), e.t.c。

现在我的问题是我想将邮件数组和 returned json 组合成一个简单的 json,我可以输出电子邮件地址和邮件数量提交总数。 "sendto" 相同的 4 个表格应该合并,这样在 4 个表格上找到的带有 9000@domain.com 的条件将合并为一个项目,以及 4 个表格上的提交总数.

类似于:

{"landingPageData":[{"landingPage":{"landingPageMail":"9000@domain.com","landingPageLeads":"1"}},{"landingPage":{"landingPageMail":"hasseris@domain.com","landingPageLeads":0}},{"landingPage":{"landingPageMail":"storcenter@domain.com","landingPageLeads":"1"}},{"landingPage":{"landingPageMail":"osteraa@domain.com","landingPageLeads":"0"}},{"landingPage":{"landingPageMail":"vejgaard@domain.com","landingPageLeads":"0"}},{"landingPage":{"landingPageMail":"gistrup@domain.com","landingPageLeads":"-"}},{"landingPage":{"landingPageMail":"storvorde@domain.com","landingPageLeads":"0"}}],"totalLeads":2})

您可以遍历 $mails_array 并使用 array_sum( array_slice() ):

对小计求和
$json = '{"arr":[1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}';

$data = json_decode( $json )->arr;
$result = array( 'landingPageData'=>[], 'totalLeads'=>0 );

foreach( $mails_array as $key => $email )
{
    $subTot = array_sum( array_slice( $data,$key*count( $formIds ), count( $formIds ) ) );
    $result['landingPageData'][] = array
    (
        'landingPage' => array
        (
            'landingPageMail'  => $email,
            'landingPageLeads' => $subTot
        )

    );
    $result['totalLeads'] += $subTot;
}

$result = json_encode( $result );

echo $result;

将打印(美化):

{
    "landingPageData": [
        {
            "landingPage": {
                "landingPageMail": "9000@domain.com ",
                "landingPageLeads": 1
            }
        },
        {
            "landingPage": {
                "landingPageMail": "hasseris@domain.com ",
                "landingPageLeads": 0
            }
        },
        {
            "landingPage": {
                "landingPageMail": "storcenter@domain.com ",
                "landingPageLeads": 1
            }
        },
        {
            "landingPage": {
                "landingPageMail": "osteraa@domain.com ",
                "landingPageLeads": 0
            }
        },
        {
            "landingPage": {
                "landingPageMail": "vejgaard@domain.com ",
                "landingPageLeads": 0
            }
        },
        {
            "landingPage": {
                "landingPageMail": "gistrup@domain.com ",
                "landingPageLeads": 0
            }
        },
        {
            "landingPage": {
                "landingPageMail": "storvorde@domain.com ",
                "landingPageLeads": 0
            }
        }
    ],
    "totalLeads": 2
}

这是核心线:

$subTot = array_sum( array_slice( $data, $key*count( $formIds ), count( $formIds ) ) );

将当前 mails_array 键乘以 $formIds 我们可以获得当前元素的起始 $data 键,然后我们可以从 $data 中提取仅与当前电子邮件相关的值和使用 array_sum() 对它们求和。总和被添加到一个新的 landingPageData 子节点并且 totalLeads 值被它递增。