$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" } }

将两个数组作为输入,尝试合并或连接两个数组并将结果存储在第三个数组中。合并两个数组的逻辑如下所示-J=0,k=0for(i=0;i<o;i++){//mergingtwoarrays if(a[j]<=b[k]){ c[i]=a[j]; j++; }else{ &nbs

在JavaScript中,合并数组是一种常见的操作,可以使用concat函数来实现。concat函数可以将多个数组合并成一个新的数组。下面我们来看一个具体的代码示例。首先,我们定义几个数组作为示例数据:vararr1=[1,2,3];vararr2=[4,5,6];vararr3=[7,8,9];接下来,使用concat函数

在php中,可以使用“array_merge_recursive()”函数来不改变键值的合并二维数组;该函数在处理两个或更多个数组元素有相同的键名的情况时,不会进行键名覆盖,而是将多个相同键名的值递归组成一个数组。

PHP中如何将多个数组按照指定键名合并为一个数组在开发中,我们经常会遇到将多个数组按照指定键名合并为一个数组的需求。这种需求在处理数据时非常常见,特别是在处理数据库结果集等情况下。本文将介绍几种常见的方法来实现这个功能,并给出相应的代码示例。方法一:使用循环遍历最简单的方法是使用循环遍历所有的数组,并根据指定键名将相应的值加入到新的数组中。示例代码如下:fu

如何将两个PHP数组合并为一个数组在PHP开发中,我们经常需要将两个数组合并为一个数组。这种操作在数据处理和数组操作中非常常见。本文将介绍如何使用PHP简单而高效地合并两个数组。PHP提供了两个函数来合并数组,分别是array_merge()和array_merge_recursive()。下面我们分别介绍这两个函数的用法和示例代码。ar

PHP函数介绍—array_combine():将两个数组合并成一个关联数组在PHP中,有许多实用的函数可以帮助我们处理和操作数组。其中一个非常有用的函数是array_combine()。本文将为大家介绍这个函数的用法及其示例代码。array_combine()函数是将一个数组的值作为键名,将另一个数组的值作为键值,将这两个数组合并成一个新的关联数组。这个

PHP中如何合并两个数组在PHP编程中,经常会遇到需要合并两个数组的情况。PHP提供了多种方法来实现数组的合并操作,本文将介绍其中的几种常用方法,并附带代码示例。方法一:使用array_merge函数array_merge函数是PHP提供的一个用于合并数组的内置函数。它接受多个数组作为参数,并返回一个合并后的新数组。以下是使用array_merge函数合并两

PHP中如何合并多个数组在PHP中,数组是一种非常重要的数据结构,经常被用于存储和操作多个相关的数据项。有时候,我们需要将多个数组合并成一个数组,以便更方便地对数据进行处理。本文将介绍如何在PHP中合并多个数组,并提供代码示例。PHP提供了多种方法可以合并数组,这里我们将介绍三种常用的方法:使用"+"操作符、使用array_merge函数和使用array_m


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use
