drflkphi675447 2014-02-12 21:50
浏览 103
已采纳

使用两个键从SQL创建多维数组

My SQL table is structured like this: class_id, class_name, day

I want to convert this into a multidimensional array structured as below

$days['day']['class_id']

My attempt so far is as follows:

while ($row = mysqli_fetch_assoc($result)) {
    $days[$row['day']] = $row;
}

This creates a second layer to the array based on the day, however I'm not sure how to develop that further to sort the rows based on class_id.

I then want to echo each row out based on the day using the id as the key using a foreach loop. This is what I have so far.

   foreach ($days['Monday'] as $id => $value) {

         echo "<div class='class'>";
         echo $value['class_name'];
         echo "</div>";

Any help would be massively helpful!

  • 写回答

1条回答 默认 最新

  • dszdiavv474681 2014-02-12 22:01
    关注
    while ($row = mysqli_fetch_assoc($result)) {
        $days[$row['day']][$row['class_id']] = $row;
    }
    

    and then use it in foreach like you wrote.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于OPENCV的人脸识别
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!