在 PHP 中从数组创建菜单

发布于 2024-11-08 02:12:00 字数 3622 浏览 2 评论 0原文

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

[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>

I have got the following pulled from an array.

[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
    )

here is the output.

    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

I am trying to put this into a menu structure as follows.

<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>

I am trying to use the php function strstr but can't figure it out. Here is my code:

 <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦断已成空 2024-11-15 02:12:00

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

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

If your variables have the values they should have (try a var_dump), the only problem I can see, is your php version. Are you sure your server is running php 5.3?

Note that the third parameter in strstr is only available in php 5.3.

九局 2024-11-15 02:12:00

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

您实际上是在告诉 php 返回字符“/”之前的所有内容。
如果您删除“true”,您将收到来自的所有内容,包括“/”字符。

你可以做类似的事情

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

by using
strstr($fname, '/', true);

you're actually telling php to return everything before the character "/".
if you drop the "true", you'll receive everything from, including the "/" character.

you can do something like

$list = substr($fname, strpos($fname, "/") +1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文