0

I need to combine two multi-dimensional arrays in order to produce a single merged array.

Array 1 ($array1) is sorted by the key value 'vo'. I need to attach a second array ($array2) to the first array with same userID.

For example:

$array1 = array(
            1 => array(
                 'id' => 1,
                 'vo' => 1,
                 'userID' => 21
           ),
           2 => array(
                 'id' => 2,
                 'vo' => 2,
                 'userID' => 22
           ),
           3 => array(
                 'id' => 3,
                 'vo' => 3,
                 'userID' => 23
           ),
           4 => array(
                 'id' => 4,
                 'vo' => 5,
                 'userID' => 25
           )
           );

Second array:

$array2 = array(
          1 => array(
                'id' => 1,
                'userID' => 21
          ),
          2 => array(
                'id' => 2,
                'userID' => 21
          ),
          3 => array(
               'id' => 3,
               'userID' => 23
          )
          );

Desired solution:

$desired_solution = array(
          1 => array(
               'id' => 1,
               'vo' => 1,
               'userID' => 21
          ),
          2 => array(
               'id' => 1,
               'userID' => 21
          ),
          3 => array(
               'id' => 2,
               'userID' => 21
          ),
          4 => array(
               'id' => 2,
               'vo' => 2,
               'userID' => 22
          ),
          5 => array(
               'id' => 3,
               'vo' => 3,
               'userID' => 23
          ),
          6 => array(
               'id' => 3,
               'userID' => 23
          ),
          ...
    );
Graham
  • 6,577
  • 17
  • 55
  • 76
Erkan Yilmaz
  • 43
  • 1
  • 7
  • 1
    StackOverflow expects you to [try to solve your own problem first](http://meta.stackoverflow.com/questions/261592), and we also [don't answer homework questions](https://softwareengineering.meta.stackexchange.com/questions/6166) (ignore if you're not asking about hw). Please update your question to show what you have already tried in a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). For further information, please see [how to ask good questions](http://stackoverflow.com/help/how-to-ask), and take the [tour of the site](http://stackoverflow.com/tour) :) – Barmar Feb 13 '18 at 22:08
  • How is the redult array supposed to be ordered? Why does the `id = 1` element from array 2 come after the corresponding element from array 1, but they're in the opposite order for `id = 2`. Maybe you can just merge the two arrays and then use `usort()` to sort the result. – Barmar Feb 13 '18 at 22:10
  • it looks like the result array is sorted by `userID`. So use `usort()` to do that. – Barmar Feb 13 '18 at 22:11
  • @Barmar: I realy tried to solve this problem for days but I don't get the expected results. I already tried usort() and all other things and don't have the intention to answer homework questions. – Erkan Yilmaz Feb 14 '18 at 09:09
  • We still expect you to show your attempt, so we can help you understand where you went wrong, rather than just do it for you. – Barmar Feb 14 '18 at 19:17

2 Answers2

1

Here is a way to produce intended result. But time complexity not good for this one.

 function search($array, $key, $value)
    {
        $results = array();

        if (is_array($array)) {
            if (isset($array[$key]) && $array[$key] == $value) {
                $results[] = $array;
            }

            foreach ($array as $subarray) {
                $results = array_merge($results, search($subarray, $key, $value));
            }
        }

        return $results;
    }

    $result = array();
    foreach($array1 as $arr){
        array_push($result, $arr);
        $id_to_search = $arr['userID'];
        $all_matched = search($array2, 'userID', $id_to_search);
        foreach($all_matched as $searched){
            array_push($result, $searched);
        }
    }

If we think a while about sorted stuff and other aspect of the array we can have better time complexity to produce intended result. Try run above code.

webDev
  • 3,868
  • 1
  • 17
  • 40
0

End result will have keys starting from 0.

<?php
$merged = array_merge($array1, $array2);
usort($merged, 'my_cmp');

function my_cmp($a, $b)
{
    $diff = $a['userID'] - $b['userID'];
    if($diff == 0)
        $diff = $a['id'] - $b['id'];
    if($diff == 0)
        $diff = isset($b['vo']);

    return $diff;
}

Sorting tips here: How can I sort arrays and data in PHP?

It's worth having a go yourself to get a feel for sorting.

Progrock
  • 6,765
  • 1
  • 16
  • 25