'Combine two multidimensional arrays in one new array

Here I have two multidimensinal arrays i need to create new array with combined data, i need to compare the "module_ids' from the first one with the key of the second array.And if i have the same value need to create the same first array but with all the QIDs included.

foreach($course_data as $k => $courses) {
    foreach($question_ids as $t => $questions) {    
        $course_data[$k]['module_ids'] =  $questions;   
    }
 }

[2]=>
  array(4) {
    ["Course_ID"]=>
    string(4) "205W"
    ["State_ID"]=>
    string(2) "AR"
    ["Trade_ID"]=>
    string(13) "Test"
    ["module_ids"]=>
    array(1) {
      ["QIDs"]=>
      string(94) "4310, 4311, 4312, 4313, 4314, 4315, 4316, 4317, 4318, 4319, 4320, 4321, 4322, 4323, 4324, 4325"
    }
  }

This is the $course_data var res

  array(4) {
    ["Course_ID"]=>
    string(4) "205W"
    ["State_ID"]=>
    string(2) "AR"
    ["Trade_ID"]=>
    string(13) "Test"
    ["module_ids"]=>
    array(1) {
      [0]=>
      array(2) {
        [0]=>
        string(4) "2127"
        [1]=>
        string(4) "2128"
      }
    }
  }

this is the $question_ids variable result

 array(2) {
        [2127]=>
            array(1) {
                ["QIDs"]=>
                string(58) "4300, 4301, 4302, 4303, 4304, 4305, 4306, 4307, 4308, 4309"
        }
        [2128]=>
            array(1) {
              ["QIDs"]=>
              string(94) "4310, 4311, 4312, 4313, 4314, 4315, 4316, 4317, 4318, 4319, 4320, 4321, 4322, 4323, 4324, 4325"
        }      
    }

need to combine both of the arrays if i find a result(same module_ids from both of the arrays) Should look something like this

 array(4) {
    ["Course_ID"]=>
    string(4) "205W"
    ["State_ID"]=>
    string(2) "AR"
    ["Trade_ID"]=>
    string(13) "Test"
    ["module_ids"]=>
      array(2) {
        [2127]=>
            array(1) {
                ["QIDs"]=>
                string(58) "4300, 4301, 4302, 4303, 4304, 4305, 4306, 4307, 4308, 4309"
        }
        [2128]=>
            array(1) {
              ["QIDs"]=>
              string(94) "4310, 4311, 4312, 4313, 4314, 4315, 4316, 4317, 4318, 4319, 4320, 4321, 4322, 4323, 4324, 4325"
        }      
    }
  }

Tried to use array_intersect to check for the values in both of the arrays but still not able to make it right.

How can I achieve this? Any help is much appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source