phparraysmerge

Merge two flat associative arrays by intersecting key to form an associative array of indexed arrays


I have two arrays of data:

array(2) {
  ["12:15"]=>
  string(84) "http://form.horseracing.betfair.com/horse-racing/010108/Catterick_Bridge-GB-Cat/1215"
  ["12:20"]=>
  string(77) "http://form.horseracing.betfair.com/horse-racing/010108/Southwell-GB-Sou/1220"
}

and

array(2) {
  ["12:15"]=>
  string(90) "http://www.racingpost.com/horses/result_home.sd?race_id=446323&r_date=2008-01-01&popup=yes"
  ["12:20"]=>
  string(90) "http://www.racingpost.com/horses/result_home.sd?race_id=446250&r_date=2008-01-01&popup=yes"
}

I want to merge these based on time, so I end up with an array of values where times in both arrays match only.

array(2) {
  ["12:15"]=>
  array(2) {
    [0]=>
    string(84) "http://form.horseracing.betfair.com/horse-racing/010108/Catterick_Bridge-GB-Cat/1215"
    [1]=>
    string(90) "http://www.racingpost.com/horses/result_home.sd?race_id=446323&r_date=2008-01-01&popup=yes"
  }
  ["12:20"]=>
  array(2) {
    [0]=>
    string(77) "http://form.horseracing.betfair.com/horse-racing/010108/Southwell-GB-Sou/1220"
    [1]=>
    string(90) "http://www.racingpost.com/horses/result_home.sd?race_id=446250&r_date=2008-01-01&popup=yes"
  }
}

Solution

  • Using a combination of the answers on offer and some extra Google foo, solved it using the following:

    $c = array_merge_recursive($a, $b);