我有两个包含计划信息的表,我想要一个整合的多维数组。这是正确获取记录的函数。有些记录可能具有相同的内容。但它们会被'类型'进一步过滤。在其他地方见面:
function getAllScheduleItems($db){
try {
$schedule_items = array();
$stmt = $db->prepare("(SELECT id, meeting_date, type, approved, expire_date FROM midweek_schedule)
UNION (SELECT id, meeting_date, type, approved, expire_date FROM weekend_schedule)
ORDER BY meeting_date ASC");
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$schedule_id = $row['id'];
// So we have a news item. Now add/create the read flag...
$schedule_items[$schedule_id]['id'] = $schedule_id;
$schedule_items[$schedule_id]['meeting_date'] = $row['meeting_date'];
$schedule_items[$schedule_id]['type'] = $row['type'];
$schedule_items[$schedule_id]['approved'] = $row['approved'];
$schedule_items[$schedule_id]['expire_date'] = $row['expire_date'];
echo "getAllScheduleItems- " . $row['meeting_date'] . "<br>";
}
}
return $schedule_items;
} catch(PDOException $ex) {
logIt($db, "ERROR", "Database request for account information failed $ex");
return 0;
}
}
其他函数调用此结果以进一步过滤我的结果:
function getAllApprovedScheduleItems($db){
$all_schedule_items = array();
$all_schedule_items = getAllScheduleItems($db);
$approved_schedule_items = array();
foreach($all_schedule_items as $record){
$schedule_id = $record['id'];
if($record['approved'] == "Y"){
$approved_schedule_items[$schedule_id]['id'] = $schedule_id;
$approved_schedule_items[$schedule_id]['type'] = $record['type'];
$approved_schedule_items[$schedule_id]['meeting_date'] = $record['meeting_date'];
$approved_schedule_items[$schedule_id]['approved'] = $record['approved'];
$approved_schedule_items[$schedule_id]['expire_date'] = $record['expire_date'];
echo "getAllApprovedScheduleItems- " . $record['meeting_date'] . "<br>";
}
}
return $approved_schedule_items;
}
最后是最后一次:
function getUnexpiredApprovedScheduleItems($db){
$all_approved_schedule_items = array();
$all_approved_schedule_items = getAllApprovedScheduleItems($db);
$unexpired_approved_schedule_items = array();
foreach($all_approved_schedule_items as $record){
$schedule_id = $record['id'];
$expire_date = $record['expire_date'];
$stale_date = new DateTime($expire_date);
$date1 = date("Y-m-d G:i:s");
$now = new DateTime($date1);
$interval = $now->diff($stale_date);
if($stale_date >= $now){
$unexpired_approved_schedule_items[$schedule_id]['id'] = $schedule_id;
$unexpired_approved_schedule_items[$schedule_id]['meeting_date'] = $record['meeting_date'];
$unexpired_approved_schedule_items[$schedule_id]['type'] = $record['type'];
$unexpired_approved_schedule_items[$schedule_id]['expire_date'] = $record['expire_date'];
$unexpired_approved_schedule_items[$schedule_id]['expire_days'] = $interval->days;
echo "getUnexpiredApprovedScheduleItems- " . $record['meeting_date'] . "<br>";
}
}
正如您所看到的,我已经嵌入了回声语句,以了解发生了什么。带有单个字母的日期的最终打印输出来自刚刚从主php代码打印的最终php返回数组。我得到以下内容:
getAllScheduleItems- 1969-12-31
getAllScheduleItems- 1969-12-31
getAllScheduleItems- 1969-12-31
getAllScheduleItems- 2017-07-02
getAllScheduleItems- 2017-07-12
getAllScheduleItems- 2017-07-16
getAllScheduleItems- 2017-07-19
getAllScheduleItems- 2017-07-23
getAllScheduleItems- 2017-07-26
getAllScheduleItems- 2017-07-30
getAllScheduleItems- 2017-08-02
getAllScheduleItems- 2017-08-06
getAllApprovedScheduleItems- 2017-07-02
getAllApprovedScheduleItems- 2017-07-16
getAllApprovedScheduleItems- 2017-07-30
getAllApprovedScheduleItems- 2017-07-19
getAllApprovedScheduleItems- 2017-07-23
getAllApprovedScheduleItems- 2017-07-26
getAllApprovedScheduleItems- 2017-08-02
getAllApprovedScheduleItems- 2017-08-06
getUnexpiredApprovedScheduleItems- 2017-07-16
getUnexpiredApprovedScheduleItems- 2017-07-30
getUnexpiredApprovedScheduleItems- 2017-07-19
getUnexpiredApprovedScheduleItems- 2017-07-23
getUnexpiredApprovedScheduleItems- 2017-07-26
getUnexpiredApprovedScheduleItems- 2017-08-02
getUnexpiredApprovedScheduleItems- 2017-08-06
2017-07-16 W
2017-07-30 W
2017-07-19 M
2017-07-23 W
2017-07-26 M
2017-08-02 M
2017-08-06 W
为什么他们出了故障?我已经研究过多维排序,但我也想了解其中的原因。哦,如果我得到一个有效的多维排序的例子,那也很棒。
答案 0 :(得分:0)
如果您有多个具有相同$schedule_id
的记录,请执行以下操作:
$schedule_items[$schedule_id]['id'] = $schedule_id;
它不会在顶级$schedule_items
数组中创建新元素,它只会覆盖数组中现有位置的$schedule_items[$schedule_id]
元素。结果是数组不会按最终结果的日期排序,它将按照每个id
的第一个项目的日期顺序排列。
如果您只想要每个id
的第一个日期,您可以检查它是否已经在数组中,而不是添加替换:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$schedule_id = $row['id'];
if (isset($schedule_items[$schedule_id]))) {
continue;
}
// So we have a news item. Now add/create the read flag...
$schedule_items[$schedule_id]['id'] = $schedule_id;
$schedule_items[$schedule_id]['meeting_date'] = $row['meeting_date'];
$schedule_items[$schedule_id]['type'] = $row['type'];
$schedule_items[$schedule_id]['approved'] = $row['approved'];
$schedule_items[$schedule_id]['expire_date'] = $row['expire_date'];
echo "getAllScheduleItems- " . $row['meeting_date'] . "<br>";
}
}
如果你想获得最后一个,你可以这样做:
unset($schedule_items[$schedule_id]);
在添加新项目之前。
您可能希望更改SQL查询,使其仅返回每个ID的第一行或最后一行,但由于UNION
,这可能会变得复杂。
答案 1 :(得分:0)
谢谢Barmar!我在考虑关联键。显然关键索引会覆盖!卫生署!我会给你正确答案,但我稍微修改了一下。我需要返回所有记录,所以我添加了一个简单的计数器来唯一标识每条记录。见下文
function getAllScheduleItems($db){
try {
$schedule_items = array();
$stmt = $db->prepare("(SELECT id, meeting_date, type, approved, expire_date FROM midweek_schedule)
UNION (SELECT id, meeting_date, type, approved, expire_date FROM weekend_schedule)
ORDER BY meeting_date ASC");
$schedule_id = 1; //Keep ALL record using a unique id
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
//$schedule_id = $row['id'];
// So we have a news item. Now add/create the read flag...
$schedule_items[$schedule_id]['id'] = $schedule_id;
$schedule_items[$schedule_id]['meeting_date'] = $row['meeting_date'];
$schedule_items[$schedule_id]['type'] = $row['type'];
$schedule_items[$schedule_id]['approved'] = $row['approved'];
$schedule_items[$schedule_id]['expire_date'] = $row['expire_date'];
echo "getAllScheduleItems- " . $row['meeting_date'] . "<br>";
$schedule_id++; //increment for each record returned
}
}
return $schedule_items;
} catch(PDOException $ex) {
logIt($db, "ERROR", "Database request for account information failed $ex");
return 0;
}
}