4

我有三张专辑 Red、Ronan 和 The Hunger Games,我想在专辑名称中显示他们的歌曲。

当前数组输出:

Array
(
    [0] => Array
        (
            [album_name] => Red
            [song_title] => 22
            [song_lyrics] => 22 Lyrics Coming Soon…


            [song_audio] => N/A
            [song_length] => 03:52
        )

    [1] => Array
        (
            [album_name] => Red
            [song_title] => All Too Well
            [song_lyrics] => All Too Well Lyrics Coming Soon…


            [song_audio] => N/A
            [song_length] => 05:29
        )

    [2] => Array
        (
            [album_name] => Red
            [song_title] => Everything Has Changed
            [song_lyrics] => Everything Has Changed Lyrics Coming Soon…


            [song_audio] => N/A
            [song_length] => 04:05
        )

    [3] => Array
        (
            [album_name] => Ronan
            [song_title] => Ronan
            [song_lyrics] => Ronan Lyrics Coming Soon…


            [song_audio] => N/A
            [song_length] => 04:25
        )

    [4] => Array
        (
            [album_name] => The Hunger Games
            [song_title] => Eyes Open
            [song_lyrics] => Eyes Open Lyrics Coming Soon…


            [song_audio] => N/A
            [song_length] => 04:04
        )

    [5] => Array
        (
            [album_name] => The Hunger Games
            [song_title] => Safe & Sound
            [song_lyrics] => Safe & Sound Lyrics Coming Soon…

            [song_audio] => N/A
            [song_length] => 04:08
        )
)

如何在数组中以这种方式获取输出:

Red
22                      22 Lyrics Coming Soon…                      N/A     03:52
All Too Well            All Too Well Lyrics Coming Soon…            N/A     05:29
Everything Has Changed  Everything Has Changed Lyrics Coming Soon…  N/A     04:05

Ronan
Ronan                   Ronan Lyrics Coming Soon…                   N/A     04:25

The Hunger Games
The Hunger Games        Eyes Open Lyrics Coming Soon…               N/A     04:04
Safe & Sound            Safe & Sound Lyrics Coming Soon…            N/A     04:08

有什么想法或建议吗?谢谢。

4

1 回答 1

3

Try this:

<?php
        $array = Array(
            Array('album_name' => 'Red', 'song_title' => 22, 'song_lyrics' => '22 Lyrics Coming Soon…', 'song_audio' => 'N/A', 'song_length' => '03:52'),
            Array('album_name' => 'Red', 'song_title' => 'All Too Well', 'song_lyrics' => 'All Too Well Lyrics Coming Soon…', 'song_audio' => 'N/A', 'song_length' => '05:29'),
            Array('album_name' => 'Red', 'song_title' => 'Everything Has Changed', 'song_lyrics' => 'Everything Has Changed Lyrics Coming Soon…', 'song_audio' => 'N/A', 'song_length' => '04:05'),
            Array('album_name' => 'Ronan', 'song_title' => 'Ronan',
                'song_lyrics' => 'Ronan Lyrics Coming Soon…',
                'song_audio' => 'N/A',
                'song_length' => '04:25'
            ),
            Array
                (
                'album_name' => 'The Hunger Games',
                'song_title' => 'Eyes Open',
                'song_lyrics' => 'Eyes Open Lyrics Coming Soon…',
                'song_audio' => 'N/A',
                'song_length' => '04:04'
            ),
            Array
                (
                'album_name' => 'The Hunger Games',
                'song_title' => 'Safe & Sound',
                'song_lyrics' => 'Safe & Sound Lyrics Coming Soon…',
                'song_audio' => 'N/A',
                'song_length' => '04:08'
            )
        );

        $temp = '';

        echo '<table cellpadding="10px">';
        foreach ($array as $key1 => $val1) {
            echo '<tr>';
            foreach ($val1 as $key2 => $val2) {
                if ($key2 == 'album_name' && $temp!= $val2) {
                    $temp = $val2;
                    echo "<td><b>" . $val2 . "</b></td>";
                    echo '</tr>';
                }
                else if ($key2 != 'album_name'){
                    echo "<td>" . $val2 . "</td>";
                }
            }
            echo '</tr>';
        }
        echo '</table>';
        ?>
于 2013-10-14T08:08:25.617 回答