dousuochu7291 2012-03-20 20:03
浏览 63
已采纳

如何从数据库创建4维数组

I'm trying to figure out how to make this code work. Basically i have used MySQL to fetch an associative array containing multiple values.

example database: database name = Products

 ----------------------------------------------------------------.
|   name     | overcategory |   category   |     subcategory     |
|  Talon     |    null      | stud welding | capacitor discharge |
| cdss m3x40 | studs/bolts  |   cd-studs   |   stainless steel   |
----------------------------------------------------------------.

I used the sentence SELECT name, overcategory, category, subcategory FROM Products WHERE 1 = 1;

In the actual database there are more entries, but they are not relevant for this question.

The result i want to output as a menu on the left hand side of the screen.

<nav>
   <h1> "$overcategory" </h1>
     <h2> "$category" </h2>
         <h3> "$subcategory" </h3>
           <a href = "">$productname </a>
           <a href = "">$productname3 </a>

   <h1> "$overcategory2" </h1>
     <h2> "$category2" </h2>
         <h3> "$subcategory2" </h3>
           <a href = "">$productname2 </a>
</nav>

i was thinking of doing this by creating a multidimensional array that would look something like this:

    $testArray = array(
        ''=>array(
              'Boltsveiseapparater'=>array(
                            'Kondensator'=>array(
                                         'Talon',
                                         'LBS-75'
                                        ),
                                    'Arc'=>array(
                                         'LBH-410',
                                         'LBH-800'
                                        )
                                  )
            ),
            'Pinner/bolter'=>array(
                                        'CD-pinner'=>array(
                                                            'rustfri'=>array(
                                                                            'cdss m3x35',
                                                                            'cdss m3x40'
                                                                            ),
                                                              'stål'=>array(
                                                                            'cdms m3x35',
                                                                            'cdms m6x35'
                                                                            ),
                                                                'Alu'=>array(
                                                                            'cdal m3x10',
                                                                            'cdal m8x80'
                                                                            )
                                                        ),
                                        'Bossinger'=>array(
                                                            'Stål'=>array(
                                                                            'M6x10 5x8',
                                                                            'M5x12 4x10'
                                                                            ),
                                                                'Alu'=>array(
                                                                            'M6x10 5x8',
                                                                            'M5x12 4x10'
                                                                            ),
                                                            'Rustfri'=>array(
                                                                            'M6x10 5x8',
                                                                            'M5x12 4x10'
                                                                            )
                                                        )
                                )

      );                                                                        

I have no idea how to do this by looping through the results. is there any way of doing this without making a giant array of doom? if not, can someone assist me in decyphering this conundrum.

Thank you in advance. BM.

  • 写回答

3条回答 默认 最新

  • drmy1050 2012-03-20 20:44
    关注

    If you sort each of the columns of your results in your SQL query, you can just loop through each result and compare it with the last entry and adjust your output based on that.

    SELECT name, overcategory, category, subcategory FROM Products ORDER BY overcategory, category, subcategory, name;
    

    Otherwise, here is one way of handling your nested array:

    <?php
    
    echo "
    ";
    echo "<nav>
    ";
    foreach ($testArray as $overcategory_name => $category_data) {
        echo "\t<h1>$overcategory_name</h1>
    ";
        foreach ($category_data as $category_name => $subcategory_data) {
            echo "\t\t<h2>$category_name</h2>
    ";
            foreach ($subcategory_data as $subcategory_name => $product_name_data) {
                echo "\t\t\t<h3>$subcategory_name</h3>
    ";
                foreach ($product_name_data as $product_name) {
                    echo "\t\t\t\t<a href = \"\">$product_name</a>
    ";
                }
            }
        }
    }
    echo "</nav>
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号