我有一个关联数组,我需要合并具有相同日期的对应key =>值对
Array
(
[0] => Array
(
[CreatedDate] => 2019-03-20
[Whreceived] => 1
)
[1] => Array
(
[CreatedDate] => 2019-02-24
[Whreceived] => 1
)
[2] => Array
(
[CreatedDate] => 2019-03-21
[Whreceived] => 1
)
[3] => Array
(
[CreatedDate] => 2019-03-20
[InProcess] => 1
)
[4] => Array
(
[CreatedDate] => 2019-03-19
[InProcess] => 1
)
[5] => Array
(
[CreatedDate] => 2019-03-25
[OnHold] => 2
)
[6] => Array
(
[CreatedDate] => 2019-03-19
[WhInwardDone] => 1
)
)
预期产量
Array
(
[0] => Array
(
[CreatedDate] => 2019-03-20
[Whreceived] => 1
[InProcess] => 1
)
[1] => Array
(
[CreatedDate] => 2019-02-24
[Whreceived] => 1
)
[2] => Array
(
[CreatedDate] => 2019-03-21
[Whreceived] => 1
)
[4] => Array
(
[CreatedDate] => 2019-03-19
[InProcess] => 1
[WhInwardDone] => 1
)
[5] => Array
(
[CreatedDate] => 2019-03-25
[OnHold] => 2
)
)
基本上,我需要具有唯一日期及其所有对应值的单个数组
答案 0 :(得分:1)
您可以使用$merged_array = array_reduce($array, function ($c, $v) {
if (($k = array_search($v['CreatedDate'], array_column($c, 'CreatedDate'))) !== false) {
$c[$k] = array_merge($c[$k], $v);
}
else {
$c[] = $v;
}
return $c;
}, array());
print_r($merged_array);
进行此操作,检查新数组中是否存在日期,如果存在,将当前值与该值合并,否则在新数组中创建一个新值。
$merged_array = array();
foreach ($array as $v) {
if (($k = array_search($v['CreatedDate'], array_column($merged_array, 'CreatedDate'))) !== false) {
$merged_array[$k] = array_merge($merged_array[$k], $v);
}
else {
$merged_array[] = $v;
}
}
或者您可以使用一个简单的foreach循环:-)
Array
(
[0] => Array
(
[CreatedDate] => 2019-03-20
[Whreceived] => 1
[InProcess] => 1
)
[1] => Array
(
[CreatedDate] => 2019-02-24
[Whreceived] => 1
)
[2] => Array
(
[CreatedDate] => 2019-03-21
[Whreceived] => 1
)
[4] => Array
(
[CreatedDate] => 2019-03-19
[InProcess] => 1
[WhInwardDone] => 1
)
[5] => Array
(
[CreatedDate] => 2019-03-25
[OnHold] => 2
)
)
输出:
Actions -> Modify
答案 1 :(得分:0)
尝试以下Easy Understand trick
:
$array_mix = array(
array("CreatedDate" => "2019-03-20","Whreceived" => 1),
array("CreatedDate" => "2019-02-24","Whreceived" => 1),
array("CreatedDate" => "2019-03-21","Whreceived" => 1),
array("CreatedDate" => "2019-03-20","InProcess" => 1),
array("CreatedDate" => "2019-03-19","InProcess" => 1),
array("CreatedDate" => "2019-03-25","OnHold" => 1),
array("CreatedDate" => "2019-03-19","WhInwardDone" => 1),
);
使用foreach
:
$Newarray = array();
foreach($array_mix as $key => $array_mix_items){
if($array_mix_items['Whreceived']){
$Newarray[$array_mix_items['CreatedDate']]['CreatedDate'] = $array_mix_items['CreatedDate'];
$Newarray[$array_mix_items['CreatedDate']]['Whreceived'] = $array_mix_items['Whreceived'];
}
if($array_mix_items['InProcess']){
$Newarray[$array_mix_items['CreatedDate']]['CreatedDate'] = $array_mix_items['CreatedDate'];
$Newarray[$array_mix_items['CreatedDate']]['InProcess'] = $array_mix_items['InProcess'];
}
if($array_mix_items['WhInwardDone']){
$Newarray[$array_mix_items['CreatedDate']]['CreatedDate'] = $array_mix_items['CreatedDate'];
$Newarray[$array_mix_items['CreatedDate']]['WhInwardDone'] = $array_mix_items['WhInwardDone'];
}
if($array_mix_items['OnHold']){
$Newarray[$array_mix_items['CreatedDate']]['CreatedDate'] = $array_mix_items['CreatedDate'];
$Newarray[$array_mix_items['CreatedDate']]['OnHold'] = $array_mix_items['OnHold'];
}
}
输出:
echo "<pre>";
print_r($Newarray);
echo "</pre>";
在pre tag
中的输出:
Array
(
[2019-03-20] => Array
(
[CreatedDate] => 2019-03-20
[Whreceived] => 1
[InProcess] => 1
)
[2019-02-24] => Array
(
[CreatedDate] => 2019-02-24
[Whreceived] => 1
)
[2019-03-21] => Array
(
[CreatedDate] => 2019-03-21
[Whreceived] => 1
)
[2019-03-19] => Array
(
[CreatedDate] => 2019-03-19
[InProcess] => 1
[WhInwardDone] => 1
)
[2019-03-25] => Array
(
[CreatedDate] => 2019-03-25
[OnHold] => 1
)
)