2010-11-07 7 views
0

J'ai deux tableaux de données:tableaux Merging PHP

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" 
} 

et

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" 
} 

Je veux fusionner ces en fonction du temps, donc je me retrouve avec un tableau de valeurs où les temps dans les deux les tableaux correspondent seulement.

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" 
    } 
} 
+1

Qu'essayez-vous d'accomplir? Peut-être que vous devriez stocker les données dans un tableau chiffré par le temps. – Keyo

+0

@Keyo merci j'ai mis à jour mes tableaux pour être claveté par le temps. –

Répondre

0

En utilisant une combinaison des réponses proposées et certaines foo Google supplémentaires, résolu à l'aide ce qui suit:

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

Est-ce que ce qui suit ne serait pas utile?

$arr1 = array('time' => '14:00', 'rp' => 'blah'); 
$arr2 = array('time' => '14:00', 'bf' => 'yadda'); 

if ($arr1['time'] === $arr2['time']) { 
    $mergedArray = array_merge($arr1, $arr2); 
} 
+0

devrait-il être == au lieu de === ?? – Thalaivar

+1

@Vinothbabu, '==' fonctionnerait, car il correspond à des valeurs, en utilisant '===' serait le même mais il correspondrait également au type, donc il n'y a pas vraiment besoin d'être spécifique. – RobertPitt

0

Je suppose que vous avez un nombre quelconque de ces matrices de temps. Il est probablement une bonne idée de stocker le tableau comme ceci:

$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; 
    } 
} 
1
$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); 
0
$arr1 = array('time' => '14:00', 'rp' => 'blah'); 
$arr2 = array('time' => '14:00', 'bf' => 'yadda'); 
$arr3 = $arr1 + $arr2 ; 

var_dump($arr3) ; 

Donne

array 
    'time' => string '14:00' (length=5) 
    'rp' => string 'blah' (length=4) 
    'bf' => string 'yadda' (length=5)