我正在尝试基于我的数据库中的字段构建动态数组.目的是让我可以比较这些数组,以确保它们不包含某些数据.这是一个赛道评分应用程序,我需要确保比赛类没有背靠背类的条目.
下面的回显输出数据,正如我预期的那样,但是,当我尝试将这些行从数据库推送到基于class_id的数组时,它会失败.
请参阅下面的输出.我在这里错过了什么?
(注意,我没有将所有数据带到这个聚会上,复制和粘贴太多了)
function getEventClasses()
{
global $db;
global $event_id;
try {
$getEventClasses = $db->prepare('SELECT class_id
FROM tbl_event_classes
WHERE event_id = :event_id
ORDER BY sort');
$getEventClasses->bindValue(':event_id', $event_id);
$getEventClasses->execute();
} catch (PDOException $ex) {
echo 'An Error occurred!<br>';
//user friendly message
$ex->getMessage();
echo $ex;
exit;
}
$eventClasses = [];
while ($r = $getEventClasses->fetch(PDO::FETCH_ASSOC)) {
$eventClasses[] = [
'class_id' => $r['class_id']
];
}
return $eventClasses;
}
function getEntries($class_id)
{
global $db;
global $event_id;
global $f;
try {
$getEntries = $db->prepare('SELECT tbl_event_entries.racer_id,
tbl_event_entries.class_id,
tbl_event_entries.event_id,
tbl_event_classes.class_type,
tbl_event_classes.points_class,
tbl_moto_order.moto_nbr
FROM tbl_moto_order
INNER JOIN (tbl_event_entries
LEFT JOIN tbl_event_classes
ON (tbl_event_entries.event_id = tbl_event_classes.event_id)
AND (tbl_event_entries.class_id = tbl_event_classes.class_id))
ON (tbl_moto_order.class_id = tbl_event_entries.class_id)
AND (tbl_moto_order.event_id = tbl_event_entries.event_id)
WHERE tbl_event_entries.event_id = :event_id
AND tbl_event_entries.racer_id != "9999"
AND tbl_event_entries.class_id = :class_id');
$getEntries->bindValue(':event_id', $event_id);
$getEntries->bindValue(':class_id', $class_id);
$getEntries->execute();
} catch (PDOException $ex) {
echo 'An Error occurred!<br>';
//user friendly message
$ex->getMessage();
echo $ex;
exit;
}
$eventEntries = [];
while ($r = $getEntries->fetch(PDO::FETCH_ASSOC)) {
$eventEntries[] = [
'racer_id' => $r['racer_id'],
'class_id' => $r['class_id'],
'event_id' => $r['event_id'],
'class_type' => $r['class_type'],
'points_class' => $r['points_class'],
'moto_nbr' => $r['moto_nbr']
];
}
return $eventEntries;
}
$eventClasses = getEventClasses();
foreach ($eventClasses as $eventClass) {
$eventEntries = getEntries($eventClass['class_id']);
foreach ($eventEntries as $eventEntry) {
echo ' class_id : ', $eventClass['class_id'],
' | racer_id : ', $eventEntry['racer_id'],
' | event_id : ', $eventEntry['event_id'],
' | class_type : ', $eventEntry['class_type'],
' | points_class : ', $eventEntry['points_class'],
' | moto_nbr : ', $eventEntry['moto_nbr'],
'<br>';
array_push($runningClasses[$eventClass['class_id']],
$eventEntry['racer_id'],
$eventEntry['event_id'],
$eventEntry['class_type'],
$eventEntry['points_class'],
$eventEntry['moto_nbr']);
}
}
echo的输出:
class_id : 17 | racer_id : 95 | event_id : 326 | class_type : A | points_class : 17 | moto_nbr : 3
class_id : 17 | racer_id : 350 | event_id : 326 | class_type : A | points_class : 17 | moto_nbr : 3
class_id : 17 | racer_id : 493 | event_id : 326 | class_type : A | points_class : 17 | moto_nbr : 3
class_id : 17 | racer_id : 295 | event_id : 326 | class_type : A | points_class : 17 | moto_nbr : 3
class_id : 14 | racer_id : 1142 | event_id : 326 | class_type : A | points_class : 14 | moto_nbr : 4
class_id : 14 | racer_id : 162 | event_id : 326 | class_type : A | points_class : 14 | moto_nbr : 4
class_id : 14 | racer_id : 151 | event_id : 326 | class_type : A | points_class : 14 | moto_nbr : 4
class_id : 14 | racer_id : 1176 | event_id : 326 | class_type : A | points_class : 14 | moto_nbr : 4
$runningclasses的内容:
Array
(
[17] => Array
(
[0] => 295
[1] => 326
[2] => A
[3] => 17
[4] => 3
)
[14] => Array
(
[0] => 1125
[1] => 326
[2] => A
[3] => 14
[4] => 4
)
)
我只在runningClasses数组中的每个eventClass中获取最后一个eventEntry.
感谢Manjeet Barnala提醒我这个..下面是我正在使用的工作foreach循环:
foreach ($eventClasses as $eventClass) {
$eventEntries = getEntries($eventClass['class_id']);
foreach ($eventEntries as $eventEntry) {
$runningClasses[$eventClass['class_id']][] = [
$eventEntry['racer_id'],
$eventEntry['event_id'],
$eventEntry['class_type'],
$eventEntry['points_class'],
$eventEntry['moto_nbr']
];
}
}
最佳答案 用你的数组push替换这段代码并声明$runningClasses = array();函数顶部的空数组..
$arr = array();
$arr['racer_id'] = $eventEntry['racer_id'];
$arr['event_id'] = $eventEntry['event_id'];
$arr['class_type'] = $eventEntry['class_type'];
$arr['points_class']= $eventEntry['points_class'];
$arr['moto_nbr']= $eventEntry['moto_nbr']);
$runningClasses[$eventClass['class_id']][] = $arr;