我正在尝试将 MySQL 结果放入一个数组中,以便为相关产品创建标题。我遇到了这个特定代码的问题。我不太明白这个错误,但我假设它与数组中的格式和结构有关。
foreach ($equipTitles as $equipTitle) {
$titles .= [$equipTitle['equipment_category_id']=>$equipTitle['equipment_name']];
//echo $titles;
}
如果我用实际数组替换有问题的代码,它会按预期工作。
$titles = ["1"=>"Audio",
"2"=>"Video",
"3"=>"Lighting",
"4"=>"Other"];
代码片段
function getEquipCat() {
global $db;
$query = "SELECT equipment_category_id, equipment_name
FROM equip_category ORDER BY equipment_category_id";
//lets prep to execute
$statement = $db->prepare($query);
$statement->execute();
$equipCat = $statement->fetchAll(PDO::FETCH_ASSOC);
$statement->closeCursor();
return $equipCat;
}
.....
$equipTitles = getEquipCat();
.....
<?php
foreach ($equipTitles as $equipTitle) {
$titles .= [$equipTitle['equipment_category_id']=>$equipTitle['equipment_name']];
//echo $titles;
}
var_dump($titles);
?>
<?php foreach ($titles as $titleV => $title) :?>
<?php echo "<h4>$title</h4>";?>
<?php foreach ($allAudioEquipment[$titleV] as $audioEquipment) :?>
<div class="divTableBody">
<div class="divTableRow">
<div class="divTableCellEquipCode"><?php echo '<input type="hidden" id="'.$audioEquipment['equip_id'].'" name="equip_id" value="'.$audioEquipment['equip_id'].'">'.$audioEquipment['equip_id']; ?> </div>
<div class="divTableCellEquipDesc"><?php echo $audioEquipment['equip_name']; ?></div>
<div class="divTableCellEquipAdd"><input class="addToFlows" type="submit" value="+" /></div>
</div><!-- End Table Row -->
</div><!-- End Table Body -->
<?php endforeach; ?>
<?php endforeach; ?>
</div><!-- End Table -->
</div>
错误:未定义变量:标题和注意:数组到字符串的转换
沧海一幻觉
回首忆惘然