0

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;
5
  • 1
    Not clear. What is the expected output? Commented Mar 13, 2020 at 10:46
  • 1
    So what have you tried so far? Please show us your attempt to reach the desired goal. Commented Mar 13, 2020 at 10:51
  • @Sougata Bose thank, your answer it's work perfect me Commented Mar 13, 2020 at 11:20
  • @Nick thank, your answer it's work perfect me Commented Mar 13, 2020 at 11:21
  • How to this array sort month and year wise ? Commented Mar 14, 2020 at 10:02

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

How to this array sort month and year wise ?
@HardikPatel that's probably worth a new question, but basically you would use uksort with a function that converted the Apr-2019 style strings into a DateTime object (using date_create_from_format) and then sorted on them.
1

You can use simple loops also to do that -

$new = [];
foreach ($array as $key =>$a) {
    $new[$key] = []; // Define with key
    foreach ($a as $v) {
        $new[$key] += $v; // Concat
    }
}

2 Comments

How to this array output sort month and year wise?
You can use usort() for sorting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.