猿问

Mysql结果成php数组,报错注意:数组到字符串的转换

我正在尝试将 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>

错误:未定义变量:标题和注意:数组到字符串的转换


阿波罗的战车
浏览 134回答 2
2回答

沧海一幻觉

.= 不是您添加到数组的方式,它用于附加到字符串。用foreach ($equipTitles as $equipTitle) {&nbsp; &nbsp; $titles[$equipTitle['equipment_category_id']] = $equipTitle['equipment_name'];}

回首忆惘然

我认为没有任何理由将您的结果集重新存储为第二个/冗余变量。只需在循环中使用结果集数据。这将允许您完全跳过foreach ($equipTitles as $equipTitle) {...}脚本部分——这是一件好事。您的代码可能如下所示:<?phpfunction getEquipCat($db)&nbsp; {&nbsp; &nbsp; return $db->query("SELECT equipment_category_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; equipment_name&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM equip_category&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ORDER BY equipment_category_id")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->fetchAll(PDO::FETCH_ASSOC);}foreach (getEquipCat($db) as $cat_row) {&nbsp; &nbsp; echo "<h4>{$cat_row['equipment_name']}</h4>";&nbsp; &nbsp; foreach ($allAudioEquipment[$cat_row['equipment_category_id']] as $audioEquipment) {&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }}请注意,我没有使用准备好的语句来查询数据库,因为在您的查询中没有要绑定的变量。如果您的项目在查询中使用变量,则保留准备好的语句语法。
随时随地看视频慕课网APP
我要回答