phparraysmultidimensional-arrayhtml-tablegrouping

Group rows of a 2d array by column value and print as an HTML table


I have three albums (Red, Ronan and The Hunger Games) and I want to display their songs under grouped album names in an HTML table.

My ungrouped data:

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

I'd like to present the album name as a row before presenting songs as rows.

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

Solution

  • 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>';
            ?>