嵌套数组至3级的无序列表 [英] Nested array to 3-level unordered list

查看:107
本文介绍了嵌套数组至3级的无序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的数组,我期待把它变成一个无序列表:

I have a nested array, and am looking to turn it into an unordered list:

[2009] => Array
    (
        [Show Name 1] => Array
            (
                [0] => Class 1
                [1] => Class 2
            )

    )

[2008] => Array
    (
        [Show Name 2] => Array
            (
                [0] => Class 1
                [1] => Class 2
            )

    )

和把它转换成:

2009
    Show Name 1
        Class 1
        Class 2
2008
    Show Name 2
        Class 1
        Class 2

到目前为止,我已经完全管理的,通过能够显示年,但在那之后我得到'阵',其中显示的名称应该是:

So far I have partially managed it, by being able to show the Year, but after that I get 'array' where the show name should be:

2012
    Array
    Array
2009
    Array
2008
    Array

我提出,使用以下内容:

I made that using the following:

    <ul>
    <?php foreach( $results as $year => $shows ): ?>
    <li><?= $year ?>
      <ul>
        <?php foreach( $shows as $show ): ?>
        <li><?= $show ?></li>
        <?php endforeach; ?>
      </ul>
    </li>
    <?php endforeach; ?>
</ul>


更新

我要抢YEAR_ID,SHOW_ID,标识码每个结果,这样我可以将它们传递到一个URL。是这样的:

I need to grab the YEAR_ID, SHOW_ID, CLASS_ID for each result, so that I can pass them into a URL. Something like:

        <ul class="no-bullet">
        <?php foreach( $results as $year => $shows ): ?>
        <li><h2><?= $year ?></h2>
          <ul class="no-bullet">
            <?php foreach( $shows as $show_name => $show ): ?>
            <li><h4><?= $show_name ?></h4></li>
                 <ul class="no-bullet">
                      <?php foreach( $show as $class ): ?>
                      <li><a href="results.html?year=$yearid&show=$showid&class=$classid"><?= $class ?></a></li>
                      <?php endforeach; ?>
                 </ul>
            <?php endforeach; ?>
          </ul>
        </li>
        <?php endforeach; ?>
    </ul>

不过,我一点都不知道从哪里开始。我认为foreach循环中的查询,但想通这可能是非常低效的?

However, I haven't the foggiest where to start. I considered a query within the foreach loop, but figured that may be quite inefficient?

推荐答案

您需要治疗 $显示作为一个数组,只提取你想要的部分,像这样的:

You need to treat the $show as an array and just extract the part you want, something like this:

<ul>
    <?php foreach( $results as $year => $shows ): ?>
    <li><?= $year ?>
      <ul>
        <?php foreach( $shows as $show_name => $show ): ?>
        <li><?= $show_name ?>
             <ul>
                  <?php foreach( $show as $class ): ?>
                  <li><?= $class ?></li>
                  <?php endforeach; ?>
             </ul>
        </li>
        <?php endforeach; ?>
      </ul>
    </li>
    <?php endforeach; ?>
</ul>

这篇关于嵌套数组至3级的无序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆