[英]combine identical values in php arrays
I have a hard one to crack. 我很难破解。
I have this array with emails: 我有这个数组的电子邮件:
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
)
And this array with form ids: 和具有表单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
)
And with this foreach I am running through every mail in the array to create a full JSON object 并通过此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);
This creates this json that is used to call an API to return number of submissions on X forms based on a condition (sendto): 这将创建此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:""}]
Where FormIds is a GUID of a particular form with submission. 其中,FormIds是具有提交形式的特定表单的GUID。 "sendto" in conditions is the mail that I want all submissions for on the particular form.
条件中的“发送至”是我要在特定表单上提交的所有邮件。
So as an example I am trying to get all submission on all 4 forms that are send to 9000@domain.com 因此,举例来说,我尝试将所有提交的所有4种表单都发送到9000@domain.com
The above json returns this, where the number is submissions on the condition: 上面的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
]
}
So that I know that the first 4 items ( 4 form ids ) in the returned JSON is for 9000@domain.com (1,0,0,0), the next 4 for hasseris@domain.com (0,0,0,0), etc 因此,我知道返回的JSON中的前4个项目(4个表单id)是9000@domain.com(1,0,0,0),接下来的4个是hasseris@domain.com(0,0,0 ,0)等
Now my problem is that I want to combine the mail array and the returned json into a simple json that I can output with the email address and the number of total submissions. 现在我的问题是我想将邮件数组和返回的json组合成一个简单的json,可以与电子邮件地址和提交总数一起输出。 The 4 forms with identical "sendto" should be combined, so that the condition with 9000@domain.com that are found on 4 forms, will be combined into a single item with the total number of submissions on the 4 forms.
应当将具有相同“ sendto”的4个表单合并,以便将在4个表单上找到的9000@domain.com的条件合并为一个项目,并在这4个表单上提交总数。
Something like: 就像是:
{"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})
You can iterate through $mails_array
and sum subtotals using array_sum( array_slice() )
: 您可以遍历
$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;
will print (prettified): 将打印(美化):
{
"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
}
This is the core line: 这是核心线:
$subTot = array_sum( array_slice( $data, $key*count( $formIds ), count( $formIds ) ) );
Multiplying current mails_array
key for $formIds
we can obtain starting $data
key for current element, then we can extract from $data
only values related with current email and sum them using array_sum()
. 将当前的
mails_array
密钥乘以$formIds
我们可以获得当前元素的起始$data
密钥,然后我们可以从$data
仅提取与当前电子邮件相关的值,并使用array_sum()
对其求和。 The sum is added to a new landingPageData
child and the totalLeads
value is incremented by it. 总和被添加到一个新的
landingPageData
孩子和totalLeads
值由它递增。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.