0

I have 5 multidimensional arrays that I want to merge together. All the different arrays have different lenght and each of the elements of the arrays have 4 keys and values, 3 of them are shared between the arrays. These 3 can be compared, and in the case that these 3 values match, then the arrays can be merged together.

As an example 3 of my arrays look like this:

array ( 0 => array("AccountID" => 9407, "year" => 2019,"month" => "November","received" => 36), 1 => array("AccountID" => 1975,"year" => 2019,"month" => "November","received" => 230),)
array ( 0 => array("AccountID" => 9407,"year" => 2019,"month" => "November","analyzed" => 138), 1 => array("AccountID" => 1975,"year" => 2019,"month" => "April","analyzed" => 1),)
array ( 0 => array("AccountID" => 9407,"year" => 2019,"month" => "November","failed" => 1), 1 => array("AccountID" => 1975,"year" => 2020,"month" => "April","failed" => 7),)

What I'm loooking for is something like this

^ array:11 [▼
  0 => array:4 [▼
    "AccountID" => 9407
    "year" => 2019
    "month" => "November"
    "received" => 36
    "analyzed" => 138
    "failed" => 1
  ]
  1 => array:4 [▼
    "AccountID" => 1975
    "year" => 2020
    "month" => "April"
    "received" => 230
    "analyzed" => 1
    "failed" => 7      ]

until now I've tried the following code, which I use to merge 2 arrays together. this i repeat for the other arrays, merging 2 at once. But it is delivering wrong values. Probably because it is only comparing if the key exist and not also the value of it.

       foreach ($received_list as $key => $received) {
        if (array_key_exists($key, $analyzed_list)) {
            $merged[$key] = array_merge($received_list[$key], $analyzed_list[$key]);
        } else {
            $merged[$key] = array_merge($received_list[$key], ['analyzed' => 0]);
        }
       }
      foreach ($analyzed_list as $key => $received) {
        if (array_key_exists($key, $received_list)) {
            $merged[$key] = array_merge($analyzed_list[$key], $received_list[$key]);
        } else {
            $merged[$key] = array_merge($analyzed_list[$key], ['received' => 0]);
        }
      }

Any kind of help is much appreciated!

1
  • Post your arrays as var_export instead. Nobody will retype all your arrays to work with the code. Commented Apr 24, 2020 at 11:31

1 Answer 1

1

Not the best performing one but looks like it works

The sample;

    $first = [
        ['AccountID' => 9407, 'year' => 2019, 'month' => 'November', 'received' => 36],
        ['AccountID' => 1975, 'year' => 2019, 'month' => 'November', 'received' => 230],
    ];
    $second = [
        ['AccountID' => 9407, 'year' => 2019, 'month' => 'November', 'analyzed' => 138],
        ['AccountID' => 1975, 'year' => 2019, 'month' => 'April', 'analyzed' => 1],
    ];
    $third = [
        ['AccountID' => 9407, 'year' => 2019, 'month' => 'November', 'failed' => 1],
        ['AccountID' => 1975, 'year' => 2020, 'month' => 'April', 'failed' => 7],
    ];

The code;

    return collect([$first, $second, $third])
        ->flatten(1)
        ->transform(function ($group) {
            $group['id'] = $group['AccountID'] . '-' . $group['year'] . '-' . $group['month'];

            return $group;
        })
        ->groupBy('id')
        ->transform(function (Collection $subGroup) {
            $merge = [];

            $subGroup->transform(function ($sub) use (&$merge) {
                unset($sub['id']);
                $merge = array_merge($sub, $merge);

                return $merge;
            });

            return $subGroup->last();
        })
        ->values()
        ->toArray();

json response;

[{"AccountID":9407,"year":2019,"month":"November","failed":1,"analyzed":138,"received":36},{"AccountID":1975,"year":2019,"month":"November","received":230},{"AccountID":1975,"year":2019,"month":"April","analyzed":1},{"AccountID":1975,"year":2020,"month":"April","failed":7}]
Sign up to request clarification or add additional context in comments.

Comments

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.