Table of Contents
回复讨论(解决方案)

合并数组

Jun 23, 2016 pm 01:15 PM
Merge arrays

 $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);
Copy after login

我这样写得出来的这样
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)
Copy after login

我想要的结果
$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'        ) * );
Copy after login


回复讨论(解决方案)

补充我的建名都是一样的。然后根据建名将值合并

    $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);
Copy after login
Copy after login
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',  ),)
Copy after login
Copy after login

    $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);
Copy after login
Copy after login
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',  ),)
Copy after login
Copy after login


版主 我想简单了。。。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',  ),)
Copy after login


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',  ),)
Copy after login

有什么区别吗?
被来就是按键关联的
你 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
Copy after login

account_log表数据 ==》记录所有的充值、消费、退款记录user_id       user_money          frozen_money       change_desc1                           10                              0                               充值1                            20                              0                              充值1                            -40                             0                              消费2                            50                              0                              充值3                            50                              0                              充值
Copy after login

user_account表   ==》   只记录充值记录user_id         amount                      payment1                        10                                 充值10卡号12321                         20                                充值20卡号53452                        50                                 充值50卡号34533                         50                             充值50卡号5585
Copy after login

我当初的想法是,将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'))
Copy after login

我最终想要这样的数据,然后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" } } 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to merge two arrays in C language? How to merge two arrays in C language? Sep 10, 2023 am 09:05 AM

Taking two arrays as input, try to merge or concatenate the two arrays and store the result in the third array. The logic of merging two arrays is as follows-J=0,k=0for(i=0;i<o;i++){//mergingtwoarrays if(a[j]<=b[k]){ c[i] =a[j]; j++; }else{ &nbs

Concatenate arrays using concat function in JavaScript Concatenate arrays using concat function in JavaScript Nov 18, 2023 pm 05:35 PM

In JavaScript, merging arrays is a common operation and can be achieved using the concat function. The concat function can combine multiple arrays into a new array. Let's look at a specific code example. First, we define several arrays as sample data: vararr1=[1,2,3]; vararr2=[4,5,6]; vararr3=[7,8,9]; Next, use the concat function

How to merge multiple arrays into one array according to specified key names in PHP How to merge multiple arrays into one array according to specified key names in PHP Jul 07, 2023 pm 04:12 PM

How to merge multiple arrays into one array according to specified key names in PHP. During development, we often encounter the need to merge multiple arrays into one array according to specified key names. This need is very common when working with data, especially when working with database result sets, for example. This article will introduce several common methods to achieve this function and give corresponding code examples. Method 1: Use loop traversal The simplest method is to use a loop to traverse all arrays and add the corresponding values ​​​​to the new array according to the specified key name. The sample code is as follows: fu

How to merge two PHP arrays into one array How to merge two PHP arrays into one array Sep 06, 2023 am 08:52 AM

How to merge two PHP arrays into one array In PHP development, we often need to merge two arrays into one array. This operation is very common in data processing and array operations. This article will introduce how to merge two arrays simply and efficiently using PHP. PHP provides two functions to merge arrays, namely array_merge() and array_merge_recursive(). Below we introduce the usage and sample code of these two functions respectively. ar

How to combine two-dimensional numbers in php without changing the key value How to combine two-dimensional numbers in php without changing the key value Sep 30, 2021 pm 03:09 PM

In PHP, you can use the "array_merge_recursive()" function to merge two-dimensional arrays without changing the key values; this function will not overwrite the key name when handling the situation where two or more array elements have the same key name. , instead, multiple values ​​with the same key name are recursively formed into an array.

PHP function introduction—array_combine(): Combine two arrays into an associative array PHP function introduction—array_combine(): Combine two arrays into an associative array Jul 24, 2023 am 08:33 AM

Introduction to PHP functions—array_combine(): Combine two arrays into an associative array. In PHP, there are many practical functions that can help us process and operate arrays. One very useful function is array_combine(). This article will introduce the usage of this function and its sample code. The array_combine() function uses the value of one array as the key name and the value of another array as the key value to merge the two arrays into a new associative array. this

How to merge two arrays in PHP How to merge two arrays in PHP Jul 07, 2023 am 09:57 AM

How to merge two arrays in PHP In PHP programming, you often encounter situations where you need to merge two arrays. PHP provides a variety of methods to implement array merging operations. This article will introduce several of the common methods, with code examples. Method 1: Use the array_merge function The array_merge function is a built-in function provided by PHP for merging arrays. It accepts multiple arrays as parameters and returns a merged new array. The following is the use of the array_merge function to merge two

How to merge multiple arrays in PHP How to merge multiple arrays in PHP Jul 08, 2023 pm 02:42 PM

How to merge multiple arrays in PHP In PHP, arrays are a very important data structure that are often used to store and operate multiple related data items. Sometimes, we need to merge multiple arrays into one array to process the data more conveniently. This article will explain how to merge multiple arrays in PHP and provide code examples. PHP provides a variety of methods to merge arrays. Here we will introduce three common methods: using the "+" operator, using the array_merge function and using array_m

See all articles