X
X
XenK2016-08-18 05:16:56
PHP
XenK, 2016-08-18 05:16:56

Merge two arrays by keys?

There is the first array:

Array
(
    [0] => Array
        (
            [date1] => 15.08.2016
            [money] => 1050
        )

    [1] => Array
        (
            [date1] => 11.08.2016
            [money] => 5000
        )

    [2] => Array
        (
            [date1] => 10.08.2016
            [money] => 600
        )

And there is a second one:
Array
(
    [0] => Array
        (
            [date1] => 15.08.2016
            [money] => 0
        )

    [1] => Array
        (
            [date1] => 14.08.2016
            [money] => 0
        )

    [2] => Array
        (
            [date1] => 13.08.2016
            [money] => 0
        )

How do I join these two arrays by keys if date1 is the same as date1 ?
What should happen:
Array
(
    [0] => Array
        (
            [date1] => 15.08.2016
            [money] => 1050
        )

    [1] => Array
        (
            [date1] => 14.08.2016
            [money] => 0
        )

    [2] => Array
        (
            [date1] => 13.08.2016
            [money] => 0
        )

    [3] => Array
        (
            [date1] => 12.08.2016
            [money] => 0
        )

    [4] => Array
        (
            [date1] => 11.08.2016
            [money] => 5000
        )

    [5] => Array
        (
            [date1] => 10.08.2016
            [money] => 600
        )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nazar Mokrinsky, 2016-08-18
@nazarpc

$merged_normalized =
    array_column($arr1, 'money', 'date1') +
    array_column($arr2, 'money', 'date1');
$result = [];
foreach ($merged_normalized as $date => $money) {
    $result[] = [
        'date'                  => $date,
        'money_products_client' => $money
    ];
}
unset($merged_normalized, $date, $money);

T
toxa82, 2016-08-18
@toxa82

array_merge()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question