![](/img/trans.png)
[英]Group array rows by column value and push whole rows as subarrays within the group
[英]Group array rows by year and month of date value and push a column value into the group
我有一個包含標題、開始時間、結束時間、開始日期的事件數組。
我正在遍歷當前的所有結果:
foreach ($events as $event) {
echo $event['title'];
}
其中列出的項目絕對沒問題。
有沒有辦法可以將以下數組分組為每年的幾個月? 例如。
2018 年 5 月
2018 年 7 月
2019 年 1 月
我的示例輸入行:
Array
(
[title] => Title 1
[start_time] => 09:00
[end_time] => 17:00
[start_date] => 2017-05-25
)
Array
(
[title] => Title 2
[start_time] => 09:00
[end_time] => 17:00
[start_date] => 2018-05-25
)
Array
(
[title] => Title 3
[start_time] => 09:00
[end_time] => 17:00
[start_date] => 2018-05-27
)
Array
(
[title] => Title 3
[start_time] => 09:00
[end_time] => 17:00
[start_date] => 2018-07-15
)
Array
(
[title] => Title 4
[start_time] => 09:00
[end_time] => 17:00
[start_date] => 2019-01-2
)
下面的代碼應該可以工作。 您將必須按年份和月份對它們進行分組,然后對它們進行排序
$dates = [];
foreach($arrayOfDates as $event){
$timestamp = strtotime($event['start_date']);
$dateSort = date('Y-m', $timestamp);
$dates[$dateSort][] = ['title' => $event['title']];
}
krsort($dates);
foreach($dates as $key => $values){
echo $key;
foreach($values as $value){
echo "* ". $value['title'];
}
}
寫下JoshKisb的建議:
$monGrp = array();
// build an array based on YYYY-MM since that is what we're grouping by
foreach ($events as $event) {
$monGrp[substr($event['start_date'], 0, 7) . '-01'][] = $event;
}
// sort by the key so it is the correct date order
ksort($monGrp);
// now loop through the grouped array and write the desired output
foreach ($monGrp as $mon => $grpEvents) {
echo "<p>" . date("F Y", strtotime($mon));
foreach ($grpEvents as $event) {
echo " - " . $event['title'] . "<br>\n";
}
}
當然! 首先,您需要確保對數組進行正確排序。 為此,最好使用PHP的usort
函數。 它接受要排序的數組和要比較其元素的函數。
usort($array, function ($a, $b) {
$aDate = strtotime($a['start_date']);
$bDate = strtotime($b['start_date']);
if ($aDate == $bDate) {
return 0;
}
return ($aDate < $bDate) ? -1 : 1;
});
更多關於usort
可以在這里找到。
數組正確排序后,即可開始在代碼中創建節。 一種簡單的處理方法是創建一個變量,該變量將跟蹤您要創建的部分。 當該節更改時,它將在循環中輸出。 顯然,這僅在您之前對數組進行過排序的情況下才有效,這就是我包括上一節的原因。
$month = null;
foreach ($events as $event) {
$newMonth = date('F', strtotime($event['start_date']) );
if($month != $newMonth) {
$month = $newMonth;
echo $month;
}
echo $event['title'];
}
實現所需目標的一種可能方法包括以下步驟:
碼:
/* ----- Group the data ----- */
# Create an array to store the data.
$grouped = [];
# Iterate over every event.
foreach ($events as $event) {
# Format and save the date as 'Month Year'.
$monthYear = date("F Y", strtotime($event["start_date"]));
# Define the key of the object as as an array, if not already defined.
$grouped[$monthYear] = $grouped[$monthYear] ?? [];
# Insert the title to the array corresponding to the formatted date.
$grouped[$monthYear][] = $event["title"];
}
/* ----- Print the data ----- */
# Iterate over every key - value.
foreach ($grouped as $key => $value) {
# Print the 'Month Year' key.
echo $key . "\n";
# Iterate over every title and print it.
foreach ($value as $title) echo "- $title\n";
# Separate each group with an empty line.
echo "\n";
}
例:
查看此示例以進行現場演示。
注意:
$grouped[$monthYear] = $grouped[$monthYear] ?? []
$grouped[$monthYear] = $grouped[$monthYear] ?? []
適用於PHP7+
。 如果您使用的是舊版本,則可以使用以下替代它:
# Check whether the key is not set.
if (!isset($grouped[$monthYear])) $grouped[$monthYear] = [];
使用一個簡單的循環將標題分組並推送到每個組中,並使用另一個循環打印您的格式化文本。
代碼:(演示)
$result = [];
foreach ($array as $row) {
$key = date('F Y', strtotime($row['start_date']));
$result[$key][] = $row['title'];
}
foreach ($result as $FY => $titles) {
printf("%s\n- %s\n\n", $FY, implode("\n- ", $titles));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.