在 PHP 中从数组创建菜单


Creating a menu from array in PHP

我从数组中提取了以下内容。

[TheBeautifulSouth/2-15 One Last Love Song.m4a] => Array
    (
        [name] => TheBeautifulSouth/2-15 One Last Love Song.m4a
        [time] => 1296612503
        [size] => 7628998
        [hash] => 4c6d9b19310ad53efccc5df2e0632e82
    )
[TheBeautifulSouth/2-16 Mirror.m4a] => Array
    (
        [name] => TheBeautifulSouth/2-16 Mirror.m4a
        [time] => 1296612568
        [size] => 8418448
        [hash] => ef371e227b410bb4c9ed1ff7f2d0d70e
    )
[TheBeautifulSouth/2-17 One God.m4a] => Array
    (
        [name] => TheBeautifulSouth/2-17 One God.m4a
        [time] => 1296612639
        [size] => 8619393
        [hash] => 80f29fbef6f469e3f150f7011a320987
    )
[TheBeautifulSouth/2-18 Blackbird On the Wire (Radio Ed.m4a] => Array
    (
        [name] => TheBeautifulSouth/2-18 Blackbird On the Wire (Radio Ed.m4a
        [time] => 1296612712
        [size] => 6776214
        [hash] => fe13ced4b9fc26b013c241219c642095
    )
[dnb/] => Array
    (
        [name] => Music/dnb/
        [time] => 1296576896
        [size] => 0
        [hash] => d41d8cd98f00b204e9800998ecf8427e
    )
[dnb/03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3] => Array
    (
        [name] => dnb/03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3
        [time] => 1296577218
        [size] => 11782889
        [hash] => 372e32ad1aff44061b65f5a76e7e805c
    )
[Music/dnb/21st Century (Feat. Cabbie)_Tantrum Desire_192.mp3] => Array
    (
        [name] => dnb/21st Century (Feat. Cabbie)_Tantrum Desire_192.mp3
        [time] => 1296577357
        [size] => 8260946
        [hash] => 7fda7e838192f5c362f71ffff300a8c3
    )
[dnb/Abort_Delta Heavy_192.mp3] => Array
    (
        [name] => dnb/Abort_Delta Heavy_192.mp3
        [time] => 1296577451
        [size] => 8080602
        [hash] => bb71d713fd77746cd7debb39407ba88f
    )

这是输出。

    TheBeautifulSouth/2-15 One Last Love Song.m4anum2
    TheBeautifulSouth/2-16 Mirror.m4anum2
    TheBeautifulSouth/2-17 One God.m4anum2
    TheBeautifulSouth/2-18 Blackbird On the Wire (Radio Ed.m4anum2
    dnb/03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3num2
    dnb/21st Century (Feat. Cabbie)_Tantrum Desire_192.mp3num2
    dnb/Better Place_Dub Zero_192.mp3num2
    dnb/Body_Shot_Tantrum_Desire_192.mp3num2

我正在尝试将其放入菜单结构中,如下所示。

<ul>
<li>
    <ul>
        TheBeautifulSouth
        <li>2-15 One Last Love Song.m4anum2</li>
        <li>2-16 Mirror.m4anum2</li>
        <li>2-17 One God.m4anum2</li>
        <li>2-18 Blackbird On the Wire (Radio Ed.m4anum2</li>
    </ul>
</li>
<li>
    <ul>
        dnb
        <li>03 Twerk feat. Yo Majesty (Sub Focus Remix).mp3num2</li>
        <li>21st Century (Feat. Cabbie)_Tantrum Desire_192.mp3num2</li>
        <li>Better Place_Dub Zero_192.mp3num2</li>
        <li>Body_Shot_Tantrum_Desire_192.mp3num2</li>
    </ul>
</li>

我正在尝试使用 php 函数 strstr,但无法弄清楚。这是我的代码:

 <ul>
        <li>list
            <ul>
                <?php foreach ($bucket_contents as $file){ 
       $fname = $file['name'];
       $list = strstr($fname, '/', true); ?>
                <li><?php echo $list; ?></li>
                <?php  } ?>
            </ul>
        </li>
    </ul>

如果你的变量有它们应该有的值(试试var_dump),我能看到的唯一问题是你的php版本。您确定您的服务器运行的是 php 5.3 吗?

请注意,strstr 中的第三个参数仅在 php 5.3 中可用。

通过使用 strstr($fname, '/', true);

你实际上是在告诉 PHP 返回字符"/"之前的所有内容。如果你去掉"true",你将收到所有内容,包括"/"字符。

你可以做这样的事情

$list = substr($fname, strpos($fname, "/") +1);