合并数组
$A = array( '1' => array( 'amount' => '100', 'amount1' =>'50', 'amount2' => '200', 'payment' => '12121212', 'payment1' => '12121212' ), '2' => array( 'amount' => '10', 'amount1' =>'5', 'amount2' => '20', 'payment' => '45454545', 'payment1' => '45454545' ) ); $B = array( '1' => array( 'user_money' => '100', 'user_money1' =>'50', 'user_money2' => '200' ), '2' => array( 'user_money' => '10', 'user_money1' =>'5', 'user_money2' => '20', 'user_money3' => '45454545' ) ); foreach( $A as $k => $v){ foreach($B as $kk => $vv){ if($k = $kk){ $vvv[] = $v+$vv; } } } var_dump($vvv);
我这样写得出来的这样
array (size=4) 0 => array (size=8) 'amount' => string '100' (length=3) 'amount1' => string '50' (length=2) 'amount2' => string '200' (length=3) 'payment' => string '12121212' (length=8) 'payment1' => string '12121212' (length=8) 'user_money' => string '100' (length=3) 'user_money1' => string '50' (length=2) 'user_money2' => string '200' (length=3) 1 => array (size=9) 'amount' => string '100' (length=3) 'amount1' => string '50' (length=2) 'amount2' => string '200' (length=3) 'payment' => string '12121212' (length=8) 'payment1' => string '12121212' (length=8) 'user_money' => string '10' (length=2) 'user_money1' => string '5' (length=1) 'user_money2' => string '20' (length=2) 'user_money3' => string '45454545' (length=8) 2 => array (size=8) 'amount' => string '10' (length=2) 'amount1' => string '5' (length=1) 'amount2' => string '20' (length=2) 'payment' => string '45454545' (length=8) 'payment1' => string '45454545' (length=8) 'user_money' => string '100' (length=3) 'user_money1' => string '50' (length=2) 'user_money2' => string '200' (length=3) 3 => array (size=9) 'amount' => string '10' (length=2) 'amount1' => string '5' (length=1) 'amount2' => string '20' (length=2) 'payment' => string '45454545' (length=8) 'payment1' => string '45454545' (length=8) 'user_money' => string '10' (length=2) 'user_money1' => string '5' (length=1) 'user_money2' => string '20' (length=2) 'user_money3' => string '45454545' (length=8)
我想要的结果
$C = array( * '1' => array( 'user_money' => '100', 'user_money1' =>'50', 'user_money2' => '200' 'amount' => '100', 'amount1' =>'50', 'amount2' => '200', 'payment' => '12121212', 'payment1' => '12121212' ), '2' => array( 'user_money' => '10', 'user_money1' =>'5', 'user_money2' => '20', 'user_money3' => '45454545' 'amount' => '10', 'amount1' =>'5', 'amount2' => '20', 'payment' => '45454545', 'payment1' => '45454545' ) * );
回复讨论(解决方案)
补充我的建名都是一样的。然后根据建名将值合并
$A = array( '1' => array( 'amount' => '100', 'amount1' =>'50', 'amount2' => '200', 'payment' => '12121212', 'payment1' => '12121212' ), '2' => array( 'amount' => '10', 'amount1' =>'5', 'amount2' => '20', 'payment' => '45454545', 'payment1' => '45454545' ) ); $B = array( '1' => array( 'user_money' => '100', 'user_money1' =>'50', 'user_money2' => '200' ), '2' => array( 'user_money' => '10', 'user_money1' =>'5', 'user_money2' => '20', 'user_money3' => '45454545' ) );foreach($A as $k=>$v) $r[] = $v + $B[$k];var_export($r);
array ( 0 => array ( 'amount' => '100', 'amount1' => '50', 'amount2' => '200', 'payment' => '12121212', 'payment1' => '12121212', 'user_money' => '100', 'user_money1' => '50', 'user_money2' => '200', ), 1 => array ( 'amount' => '10', 'amount1' => '5', 'amount2' => '20', 'payment' => '45454545', 'payment1' => '45454545', 'user_money' => '10', 'user_money1' => '5', 'user_money2' => '20', 'user_money3' => '45454545', ),)
$A = array( '1' => array( 'amount' => '100', 'amount1' =>'50', 'amount2' => '200', 'payment' => '12121212', 'payment1' => '12121212' ), '2' => array( 'amount' => '10', 'amount1' =>'5', 'amount2' => '20', 'payment' => '45454545', 'payment1' => '45454545' ) ); $B = array( '1' => array( 'user_money' => '100', 'user_money1' =>'50', 'user_money2' => '200' ), '2' => array( 'user_money' => '10', 'user_money1' =>'5', 'user_money2' => '20', 'user_money3' => '45454545' ) );foreach($A as $k=>$v) $r[] = $v + $B[$k];var_export($r);
array ( 0 => array ( 'amount' => '100', 'amount1' => '50', 'amount2' => '200', 'payment' => '12121212', 'payment1' => '12121212', 'user_money' => '100', 'user_money1' => '50', 'user_money2' => '200', ), 1 => array ( 'amount' => '10', 'amount1' => '5', 'amount2' => '20', 'payment' => '45454545', 'payment1' => '45454545', 'user_money' => '10', 'user_money1' => '5', 'user_money2' => '20', 'user_money3' => '45454545', ),)
版主 我想简单了。。。key不是11对应的。这种情况如何合并啊?
array ( 16128 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16128', ), 16127 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16127', ), 16126 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16126', ), 16125 => array ( 'user_money' => '0.00', 'change_desc' => '订单 2016022647848 赠送的积分', 'user_id' => '16125', ), 16124 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16124', ), 16123 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16123', ), 16122 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16122', ), 16121 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16121', ), 16120 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16120', ), 16119 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16119', ), 16118 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16118', ), 16117 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16117', ), 16116 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16116', ), 16115 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16115', ), 16114 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16114', ), 16113 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16113', ), 16112 => array ( 'user_money' => '0.00', 'change_desc' => '注册送积分', 'user_id' => '16112', ), 16111 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16111', ), 16110 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16110', ), 16109 => array ( 'user_money' => '0.00', 'change_desc' => '', 'user_id' => '16109', ),)
B
array ( 16127 => array ( 'amount' => '1000.00', 'payment' => '储值卡号:16011118067599', 'user_id' => '16127', ), 16110 => array ( 'amount' => '1000.00', 'payment' => '储值卡号:16011128883958', 'user_id' => '16110', ), 16102 => array ( 'amount' => '1000.00', 'payment' => '储值卡号:16011125271413', 'user_id' => '16102', ), 16077 => array ( 'amount' => '500.00', 'payment' => '储值卡号:16011138443550', 'user_id' => '16077', ), 16062 => array ( 'amount' => '5.00', 'payment' => '储值卡号:15122274342772', 'user_id' => '16062', ), 16048 => array ( 'amount' => '500.00', 'payment' => '储值卡号:16011149520320', 'user_id' => '16048', ), 16037 => array ( 'amount' => '1000.00', 'payment' => '储值卡号:16011133169060', 'user_id' => '16037', ), 16029 => array ( 'amount' => '500.00', 'payment' => '储值卡号:16011127482943', 'user_id' => '16029', ), 16006 => array ( 'amount' => '500.00', 'payment' => '储值卡号:16011188176126', 'user_id' => '16006', ),)
有什么区别吗?
被来就是按键关联的
你 foreach($A as $k=>$v) $r[ $k] = $v + $B[$k]; 就可以看到
版主我的需求是这样的:我要将用户的消费信息与充值信息导出到excel中。应用到3个表,users、account_log、user_account表。
users表中只需要一个user_name字段,
account_log中需要change_desc,user_money,frozen_money。
user_account表中需要payment,amount 三个表的关联字段就是user_id。
users表数据user_id user_name1 ss2 dd
account_log表数据 ==》记录所有的充值、消费、退款记录user_id user_money frozen_money change_desc1 10 0 充值1 20 0 充值1 -40 0 消费2 50 0 充值3 50 0 充值
user_account表 ==》 只记录充值记录user_id amount payment1 10 充值10卡号12321 20 充值20卡号53452 50 充值50卡号34533 50 充值50卡号5585
我当初的想法是,将user_account表的数据查出来为A将account_log数据查出为B,然后合并数组,最终想要
数组(0 => array( 'user_id' => '1', 'user_money' => '10' , 'user_money' => '20' , 'user_money' => '-40' , 'payment'=> ‘充值10卡号1232’, ‘payment’ =>'充值20卡号5345' ),1 => array( 'user_id' => '2', 'user_money' => '50' , 'payment'=>'充值50卡号3453'),2=>array( 'user_id' => '3', 'user_money' => '50' , 'payment'=>'充值50卡号5585'))
我最终想要这样的数据,然后foreach数组,将它导出到excel。
但是我发现合并数组我合并不好。。。版主这种导出应该怎么弄?
数组的键是唯一的,你
0 => array(
'user_id' => '1',
' user_money' => '10' ,
' user_money' => '20' ,
'user_money' => '-40' ,
' payment'=> ‘充值10卡号1232’,
‘ payment’ =>'充值20卡号5345'
),
这样的数组,是不可能存在的
你可以改为三级数组啊
0 => array(
'user_id' => '1',
'user_money' =>array( '10', '20' , '-40' ),
'payment'=>array( ‘充值10卡号1232’,'充值20卡号5345' ) //这边对应user_money应该是3个值吧,
),
1 => array(
'user_id' => '2',
'user_money' => '50' ,
'payment'=>'充值50卡号3453'
),
2=>array(
'user_id' => '3',
'user_money' => '50' ,
'payment'=>'充值50卡号5585'
)
然后依据原理合并就可以
$a=array(
0 => array(
'user_id' => '1',
'user_money' =>array( '10', '20', '-40' ),
'payment'=>array('充值10卡号1232','充值20卡号5345') //这边对应user_money应该是3个值吧,
),
1 => array(
'user_id' => '2',
'user_money' => '50',
'payment'=>'充值50卡号3453',
),
2=>array(
'user_id' => '3',
'user_money' => '50',
'payment'=>'充值50卡号5585',
)
);
$b=array_merge_recursive($a[0],$a[1]);
$b=array_merge_recursive($b,$a[2]);
var_dump($b);
得到结果
array(3) { ["user_id"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } ["user_money"]=> array(5) { [0]=> string(2) "10" [1]=> string(2) "20" [2]=> string(3) "-40" [3]=> string(2) "50" [4]=> string(2) "50" } ["payment"]=> array(4) { [0]=> string(18) "充值10卡号1232" [1]=> string(18) "充值20卡号5345" [2]=> string(18) "充值50卡号3453" [3]=> string(18) "充值50卡号5585" } }
下一篇: ColaPHP 2.0α发布