php 俩个二维数组合并,根据相同的值,去合并数组

$test=array('0'=>array('days'=>20181005,'openup'=>1,'shenhe'=>0,'rejection'=>0,'adopt... $test = array( '0' => array( 'days' => 20181005, 'openup' => 1, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ), '1' => array( 'days' => 20181006, 'openup' => 3, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ), '2' => array( 'days' => 20181007, 'openup' => 1, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ), '3' => array( 'days' => 20181008, 'openup' => 1, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ), '4' => array( 'days' => 20181009, 'openup' => 5, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ), );$data = array( '0' => array( 'days' => 20181006, 'shenhe' => 5, 'rejection' => 0, 'adopt' => 3 ), '1' => array( 'days' => 20181008, 'shenhe' => 4, 'rejection' => 2, 'adopt' => 1 ), );得到结果 要是是这样的 展开
 我来答
阳光上的桥
2018-10-09 · 知道合伙人软件行家
阳光上的桥
知道合伙人软件行家
采纳数:21424 获赞数:65806
网盘是个好东东,可以对话和传文件

向TA提问 私信TA
展开全部

不知道你有没有想过,数组不要使用0、1、2、....这些下标,而是使用主键作为下标,例如:

$test = array (
    '20181005' => array('openup'=>1,'shenhe'=>5,'rejection'=>0,'adopt'=>1),
    '20181006' => array('openup'=>1,'shenhe'=>5,'rejection'=>0,'adopt'=>1),
    '20181007' => array('openup'=>1,'shenhe'=>5,'rejection'=>0,'adopt'=>1)
);

这样是不是怎么合并都简单了呢

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
237428417
2018-10-09
知道答主
回答量:7
采纳率:0%
帮助的人:1.6万
展开全部
$test = array( '0' => array( 'days' => 20181005, 'openup' => 1, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ), '1' => array( 'days' => 20181006, 'openup' => 3, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ), '2' => array( 'days' => 20181007, 'openup' => 1, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ), '3' => array( 'days' => 20181008, 'openup' => 1, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ), '4' => array( 'days' => 20181009, 'openup' => 5, 'shenhe' => 0, 'rejection' => 0, 'adopt' => 0 ));
$data = array( '0' => array( 'days' => 20181006, 'shenhe' => 5, 'rejection' => 0, 'adopt' => 3 ), '1' => array( 'days' => 20181008, 'shenhe' => 4, 'rejection' => 2, 'adopt' => 1));

$arrTest = array_merge($test,$data);
$result = array();
foreach($arrTest as $val){
$key = $val['days'];
if(!isset($result[$key])){
$result[$key] = $val;
}else{
$result[$key]['shenhe'] += $val['shenhe'];
$result[$key]['rejection'] += $val['rejection'];
$result[$key]['adopt'] += $val['adopt'];
}
}
print_r(array_values($result));
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式