我很难破解。
我有这个数组的电子邮件:
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*/', '$1:', $json);
这创建了这个json,用于调用API以根据条件(sendto)返回X表单上的提交数量:
[{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"条件是我希望在特定表单上提交所有提交的邮件。
举个例子,我试图在发送到9000@domain.com的所有4个表单上提交所有提交内容
上面的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
]
}
因此我知道返回的JSON中的前4项(4个表单ID)是9000@domain.com(1,0,0,0),接下来的4个是hasseris@domain.com(0, 0,0,0)等
现在我的问题是我想将邮件数组和返回的json组合成一个简单的json,我可以输出电子邮件地址和提交的总数。 4个表格相同" sendto"应该合并,以便在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})
答案 0 :(得分:1)
您可以使用$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
值会增加。