我需要弄清楚让我困惑的逻辑。 我有一个看起来像这样的数组。
array (
0 => array (
'invoice_id' => 'WUI_588'
'email' => 'abc@hotmail.com'
),
1 => array (
'invoice_id' => 'WUI_588'
'email' => 'def@hotmail.com'
),
2 => array (
'invoice_id' => 'WUI_589'
'email' => 'ninja@hotmail.com'
),
3 => array (
'invoice_id' => 'WUI_589'
'email' => 'ghi@hotmail.com'
),
4 => array (
'invoice_id' => 'WUI_590'
'email' => '123@hotmail.com'
),
5 => array (
'invoice_id' => 'WUI_590'
'email' => '123@hotmail.com'
),
6 => array (
'invoice_id' => 'WUI_591'
'email' => '456@hotmail.com'
),
7 => array (
'invoice_id' => 'WUI_591'
'email' => '456@hotmail.com'
),
)
因此,如果我要在表格布局中打印数组,我会得到这样的结果: -
INDEX | INVOICE ID | EMAIL
0 | WUI_588 | abc@hotmail.com
1 | WUI_588 | def@hotmail.com
2 | WUI_589 | ninja@hotmail.com
3 | WUI_589 | ghi@hotmail.com
我想要的是这样的: -
INDEX | INVOICE ID | EMAIL
0 | WUI_588 | abc@hotmail.com, def@hotmail.com
1 | WUI_589 | ninja@hotmail.com, ghi@hotmail.com
....等等。 我做的一个尝试就是这个,
foreach ($array as $key => $value) {
$invoiceidbucket[] = $value['invoice_id'];
if(in_array($value['invoice_id'], $invoiceidbucket)) {
$value['merged_email'][] = $value['email'];
//Clearing array
$invoiceidbucket = array;
}
}
答案 0 :(得分:2)
使用以下代码创建所需的数组: -
$final_array = array();
foreach($initial_array as $arr){
$final_array[$arr['invoice_id']]['invoice_id'] = $arr['invoice_id'];
$final_array[$arr['invoice_id']]['email'] = (!empty($final_array[$arr['invoice_id']]['email']))?$final_array[$arr['invoice_id']]['email'].','.$arr['email'] : $arr['email'];
}
$final_array = array_values($final_array);
echo "<pre/>";print_r($final_array);
输出: - https://eval.in/785516
注意: - 现在在循环中使用此$final_array
(foreach()
)并以所需方式打印
答案 1 :(得分:0)
这样做:
$a = [];//Your array;
$retArray = []; //Output Array
foreach ($a as $key => $val) {
$retArray['invoice_id'][] = $retArray['email'];
}
在循环结束时,您将所有invoice_ids作为其电子邮件ID数组的关键字。
在此之后无论如何打印:)
答案 2 :(得分:0)
在开始新行之前检查发票ID列循环。如果找到匹配项,请将电子邮件放在同一行,另一个检查将添加逗号,以防它不是该行的第一个电子邮件ID。
可以告诉我们你是如何找到它的。
答案 3 :(得分:0)
我的方法没有做不必要的迭代覆盖。我推荐这个:
代码:(Demo)
$array=[
['invoice_id'=>'WUI_588','email'=>'abc@hotmail.com'],
['invoice_id'=>'WUI_588','email'=>'def@hotmail.com'],
['invoice_id'=>'WUI_589','email'=>'ninja@hotmail.com'],
['invoice_id'=>'WUI_589','email'=>'ghi@hotmail.com'],
['invoice_id'=>'WUI_590','email'=>'123@hotmail.com'],
['invoice_id'=>'WUI_590','email'=>'123@hotmail.com'],
['invoice_id'=>'WUI_591','email'=>'456@hotmail.com'],
['invoice_id'=>'WUI_591','email'=>'456@hotmail.com']
];
foreach($array as $row){ // iterate all rows
if(!isset($result[$row['invoice_id']])){ // if first occurrence of invoice_id...
$result[$row['invoice_id']]=$row; // save the full row with invoice_id as the temporary key
}else{ // if not the first occurrence of invoice_id...
$result[$row['invoice_id']]['email'].=", {$row['email']}"; // concatenate the email value
}
}
var_export(array_values($result));
输出:
array (
0 =>
array (
'invoice_id' => 'WUI_588',
'email' => 'abc@hotmail.com, def@hotmail.com',
),
1 =>
array (
'invoice_id' => 'WUI_589',
'email' => 'ninja@hotmail.com, ghi@hotmail.com',
),
2 =>
array (
'invoice_id' => 'WUI_590',
'email' => '123@hotmail.com, 123@hotmail.com',
),
3 =>
array (
'invoice_id' => 'WUI_591',
'email' => '456@hotmail.com, 456@hotmail.com',
),
)