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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog