How to concatenate two array in php

Hardik Patel :

How to concatenate two arrays to a single array? In date position 0 and 1 are both concatenated in loop, my array code below here.

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
                )
        )
) 

like my output:

[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
        )

I have tried to use this code,

$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;
Nick :

You can iterate over your array, using array_merge with the splat operator ... to flatten the internal arrays. Note you can't have two DateUser keys in an array so one will be deleted; assuming they have the same values as in your data that will not be a problem:

$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);

Output:

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
        )    
)

Demo on 3v4l.org

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=346803&siteId=1