0
我有有比賽名稱,比賽ID種族的表,比賽日期。PHP數組合並問題
我想建立一個單個陣列(它是一個單一的陣列通過函數調用返回),這將包含以下格式的比賽名單:
,,---2017---
The Resolution AR, 58, 1.14.2017
Heartbreaker AR, 59, 2.11.2017
,,---2016---
The Blue Ridge AR, 38, 5.21.2016
The Fathers Day AR, 43, 6.18.2016
Adventure Bike (Santos), 54, 7.30.2016
,,---2015---
Gemini Springs AR, 4, 12.14.2015
我差不多了!但我有一個問題。
這是當前的代碼,我有:
function get_races() {
// get all the years that have races
$years = $wpdb->get_results("
select DATE_FORMAT(r.race_date,'%Y') year
from race_calendar r
group by DATE_FORMAT(r.race_date,'%Y')
order by DATE_FORMAT(r.race_date,'%Y') desc;
");
// loop through all years that have races
foreach ($years as $year) {
// add year header to array
$year_header = array(array ('','','---'.$year->year.'---'));
// get races for this particular year
$races = $wpdb->get_results("
select r.race_name
,r.race_id
,date_format(r.race_date,'%c.%d.%Y') race_date
from race_calendar r
where DATE_FORMAT(r.race_date,'%Y') = ".$year->year."
order by r.race_date;
", ARRAY_N);
// merge the year header and the races into an array
$merged_arr = array_merge($year_header, $races);
}
return $merged_arr;
}
// call function to get list of races
$races = get_races();
// display the list of races (with the year separator)
foreach ($races as $race)
{
echo $race['0'] . ',' . $race['1'] . ',' . $race['2'] . '<br />';
}
它的工作原理。但問題是,上面顯示的代碼顯示年的最後一次迭代循環,在這種情況下,2015年:
,,---2015---
Gemini Springs AR, 4, 12.14.2015
顯然$ merged_arr是開始與年循環
的每個迭代復位,這樣在功能上造成數組包含了年循環的所有迭代的數據如何更新它?
完美!謝謝! – mannyotr