为了账号安全,请及时绑定邮箱和手机立即绑定

如何在php中连接两个数组

如何在php中连接两个数组

PHP
红糖糍粑 2022-10-28 16:34:54
如何将两个数组连接到一个数组?在日期位置 0 和 1 都在循环中连接,我的数组代码在下面。Array(    [Apr-2019] => Array        (            [0] => Array                (                    [DateUser] => Apr-2019                    [withdarw_amount] => 4.00                )            [1] => Array                (                    [current_deposit_amount] => 1.00                    [current_deposit_table_refer] => 0.00                    [current_deposit_user_refer] => 0.10                    [DateUser] => Apr-2019                )        )) 像我的输出:[Apr-2019] => Array        (                    [DateUser] => Apr-2019                    [withdarw_amount] => 4.00                    [current_deposit_amount] => 1.00                    [current_deposit_table_refer] => 0.00                    [current_deposit_user_refer] => 0.10                    [DateUser] => Apr-2019        )我曾尝试使用此代码,$data = array_merge($withdrow_amount,$data_casback,$cashbonus_data,$data_discount,$CurrentDeposit);$months = array();foreach($data as $date) {  $month = substr($date['DateUser'], 0, 8);  $months[$month][] = $date;}echo '<pre>'; print_r($months); die;
查看完整描述

2 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

您可以迭代您的数组,使用array_mergesplat运算符...来展平内部数组。请注意,数组中不能有两个DateUser键,因此将删除一个;假设它们具有与您的数据相同的值,这不会成为问题:

$array = array (

    'Apr-2019' => 

    array (

        0 => 

        array (

            'DateUser' => 'Apr-2019',

            'withdarw_amount' => 4.00

        ),

        1 => 

        array (

            'current_deposit_amount' => 1.00,

            'current_deposit_table_refer' => 0.00,

            'current_deposit_user_refer' => 0.10,

            'DateUser' => 'Apr-2019'

        ),

    ),

    'Jun-2019' => 

        array (

            0 => 

            array (

                'DateUser' => 'Jun-2019',

                'withdarw_amount' => 334.00

            ),


        )

);


foreach ($array as &$arr) {

    $arr = array_merge(...$arr);

}

print_r($array);

输出:


Array

(

    [Apr-2019] => Array

        (

            [DateUser] => Apr-2019

            [withdarw_amount] => 4

            [current_deposit_amount] => 1

            [current_deposit_table_refer] => 0

            [current_deposit_user_refer] => 0.1

        )

    [Jun-2019] => Array

        (

            [DateUser] => Jun-2019

            [withdarw_amount] => 334

        )    

)

3v4l.org 上的演示


查看完整回答
反对 回复 2022-10-28
?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

您也可以使用简单的循环来做到这一点 -


$new = [];

foreach ($array as $key =>$a) {

    $new[$key] = []; // Define with key

    foreach ($a as $v) {

        $new[$key] += $v; // Concat

    }

}


查看完整回答
反对 回复 2022-10-28
  • 2 回答
  • 0 关注
  • 130 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号