我有两个数据数组:
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"
}
和
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"
}
我想根据时间合并这些,所以我最终得到一个值数组,其中两个数组中的时间仅匹配。
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"
}
}
下面的做法不会骗人吗?
$arr1 = array('time' => '14:00', 'rp' => 'blah');
$arr2 = array('time' => '14:00', 'bf' => 'yadda');
if ($arr1['time'] === $arr2['time']) {
$mergedArray = array_merge($arr1, $arr2);
}
$result = array();
foreach ($all_arrays as $a) {
if (!isset($result[$a["time"]])) {
$result[$a["time"]] = array();
}
$result[$a["time"]] = array_merge($result[$a["time"]], $a);
}
$result = array_values($result);
我假设您有任意数量的这些时间数组。像这样存储数组可能是个好主意:
$times = array(
'14:00' => array(...),
'15:00' => array(...),
etc...
);
$temp = array();
foreach($time_arrays as $time_array) {
if(isset($temp[$time_array['time'])) {
$temp[$time_array['time'] = array_merge(temp[$time_array['time'], $time_array);
}
else {
$temp[$time_array['time'] = $time_array;
}
}
$arr1 = array('time' => '14:00', 'rp' => 'blah');
$arr2 = array('time' => '14:00', 'bf' => 'yadda');
$arr3 = $arr1 + $arr2 ;
var_dump( $arr3 ) ;
给予
array
'time' => string '14:00' (length=5)
'rp' => string 'blah' (length=4)
'bf' => string 'yadda' (length=5)
结合使用提供的答案和一些额外的 Google foo,使用以下方法解决了这个问题:
$c = array_merge_recursive($a, $b);