doufei8250
2015-05-09 19:16
浏览 32
已采纳

PHP - 从php数组构建多级HTML菜单,组合基于值名称的值

I have searched a bit on SO and haven't been able to find this unique use case, but maybe I don't know how to phrase it. I am looking to use a PHP array (see format below) to build a dynamic HTML menu that duplicates identical string values into sub menus when a match exists.

This may be too complicated but I would like to find out if it is possible and if someone has a code sample. I don't mind changing the 'verse' array value structure to make it more mutable and get this to work (perhaps turning it into array since there are 1:many potential values in it.)

I could not get the HTML below to format quite right for SO but the gist of the request is that all articles with a matching book/chapter string would get placed together and nested. If there are 2 entries for Psalms 121 (which there are in the below array), then they should be nested together in the same sub-menu.

Desired End Structure (Note that because 2 strings matched Psalms chapter 121, they were combined together:

Hebrews
    Chapter 12
        Verse 26-28
    Chapter 9
        Verse 23-24

Psalms
    Chapter 121
         Verse 3-4
         Verse 7-10
    Chapter 16
         Verse 11

Exodus
    Chapter 33
         Verse 14

Thank you in advance for your help!

PHP Array:

<?php

$articles = array(

array(
    'contentid' => '109',
    'full' => 'Song by Artist (Ref 109)',
    'verse' => 'Hebrews 12:26-28'),

array(
    'contentid' => '110',
    'full' => 'Song by Artist (Ref 110)',
    'verse' => 'Psalms 121:3-4'),

array(
    'contentid' => '111',
    'full' => 'Song by Artist (Ref 111)',
    'verse' => 'Hebrews 9:23-24'),

array(
    'contentid' => '112',
    'full' => 'Song by Artist (Ref 112)',
    'verse' => 'Psalms 16:11; Exodus 33:14; Psalms 121:7-10'));

?>

I would like this to draw an HTML menu that looks like this:

<!-- Hebrews -->
                    <div class="submenu">
                        <a href="#">**Hebrews**</a>
                        <!-- Level 2 menu -->
                        <div>
                            <div>


                                <div class="submenu">
                                    <a href="#">Chapter *12*</a>
                                    <!-- Level 3 menu -->
                                    <div>
                                        <div>
                                            <a href="#"><span>Song by Artist (Ref 109)</span>Verse *26-28*</a>
                                    </div>
                                    </div>
                                </div>

                                <div class="submenu">
                                    <a href="#">Chapter *9*</a>
                                    <!-- Level 3 menu -->
                                    <div>
                                        <div>
                                            <a href="#"><span>Song by Artist (Ref 111)</span>Verse *23-24*</a>
                                        </div>
                                    </div>
                                </div>

<!-- Psalms -->

                    <div class="submenu">
                        <a href="#">**Psalms**</a>
                        <!-- Level 2 menu -->
                        <div>
                            <div>

                                <div class="submenu">
                                    <a href="#">Chapter *121*</a>
                                    <!-- Level 3 menu -->
                                    <div>
                                        <div>
                                            <a href="#"><span>Song by Artist (Ref 110)</span>Verse *3-4*</a>
                                            <a href="#"><span>Song by Artist (Ref 112)</span>Verse *7-10*</a>                                       </div>
                                    </div>
                                </div>
Chapter *9* Song by Artist (Ref 111)Verse *23-24*
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douji6199 2015-05-09 20:25
    已采纳
    $articles = array
    (
        array
        (
            'contentid' => '109',
            'full' => 'Song by Artist (Ref 109)',
            'verse' => 'Hebrews 12:26-28'
        ),
    
        array
        (
            'contentid' => '110',
            'full' => 'Song by Artist (Ref 110)',
            'verse' => 'Psalms 121:3-4'
        ),
    
        array
        (
            'contentid' => '111',
            'full' => 'Song by Artist (Ref 111)',
            'verse' => 'Hebrews 9:23-24; Psalms 121:3-4'
        ),
    
        array
        (
            'contentid' => '112',
            'full' => 'Song by Artist (Ref 112)',
            'verse' => 'Psalms 16:11; Exodus 33:14; Psalms 121:7-10; Psalms 121:3-4'
        )
    );
    
    
    
    $langs = array();
    
    foreach($articles as $article)
    {
        foreach(explode("; ", $article["verse"]) as $verseData)
        {
            $verse = explode(" ", $verseData);
            $lang = $verse[0];
            $fullVerse = $verse[1];
            $verse = explode(":", $verse[1]);
            $chapter = $verse[0];
    
            if(empty($langs[$lang]))
                $langs[$lang] = array();
    
            if(empty($langs[$lang][$fullVerse]))
                $langs[$lang][$fullVerse] = array();
    
            $langs[$lang][$fullVerse][] = array
            (
                "full" => $article["full"]
            );
        }
    }
    
    foreach($langs as $lang => $data)
    {
        generateHtml($lang, $data);
    }
    
    function generateHtml($lang, $data)
    {
        echo "<!-- ". $lang ." -->
    ";
        echo "<div class=\"submenu\">
    ";
        echo "<a href=\"#\">**". $lang ."**</a>
    ";
        echo "<!-- Level 2 menu -->
    ";
        echo "<div>
    ";
        echo "<div>
    ";
    
        foreach($data as $fullVerse => $verses)
        {
            $verse = explode(":", $fullVerse);
            $chapter = $verse[0];
            $verse = $verse[1];
    
            echo "<div class=\"submenu\">
    ";
            echo "<a href=\"#\">Chapter *". $chapter ."*</a>
    ";
            echo "<!-- Level 3 menu -->
    ";
            echo "<div>
    ";
            echo "<div>
    ";
    
            foreach($verses as $verseIndex => $value)
            {
                if(count($verses) > 1)
                    $value["full"] .= " (". ($verseIndex + 1) .")";
    
                echo "<a href=\"#\"><span>". $value["full"] ."</span>Verse *". $verse ."*</a>
    ";
            }
    
            echo "</div>
    ";
            echo "</div>
    ";
        }
    
        echo "</div>
    ";
        echo "</div>
    ";
        echo "</div>
    ";
    }
    

    You can use \t to also add the tab space. My suggestion is if you already generating it like this. make it one line by removing all the .

    点赞 评论

相关推荐 更多相似问题