"Alice's Restaurant", "artist" => "Arlo Guthrie", "cover" => "/stuff/albums/alicesrestaurant.jpg", "tags" => "folk americana cd 1960s" ), array( "title" => "Speaking in Tongues", "artist" => "Talking Heads", "cover" => "/stuff/albums/tongues.jpg", "tags" => "rock new-wave vinyl 1980s" ), array( "title" => "To The Faithful Departed", "artist" => "The Cranberries", "cover" => "/stuff/albums/faithfuldeparted.jpg", "tags" => "rock alternative cd 1990s" ), array( "title" => "good kid, m.A.A.d city", "artist" => "Kendrick Lamar", "cover" => "/stuff/albums/maad.jpg", "tags" => "hip-hop cd 2010s" ), array( "title" => "Moving Pictures", "artist" => "Rush", "cover" => "/stuff/albums/movingpictures.jpg", "tags" => "rock progressive vinyl cd 1980s" ), array( "title" => "Grace Under Pressure", "artist" => "Rush", "cover" => "/stuff/albums/gp.jpg", "tags" => "rock progressive cd 1980s" ), array( "title" => "Splendido Hotel", "artist" => "Al Di Meola", "cover" => "/stuff/albums/Splendido_hotel.jpg", "tags" => "jazz fusion vinyl 1980s" ), array( "title" => "Rumours", "artist" => "Fleetwood Mac", "cover" => "/stuff/albums/rumours.jpg", "tags" => "rock pop vinyl 1970s" ), array( "title" => "Diva", "artist" => "Annie Lennox", "cover" => "/stuff/albums/diva.jpg", "tags" => "pop cd 1990s" ), array( "title" => "Street Lady", "artist" => "Donald Byrd", "cover" => "/stuff/albums/streetlady.jpg", "tags" => "jazz fusion funk cd 1970s" ) ); function splitTags($tags) { $tagArray = explode(" ", $tags); return $tagArray; } function printTags($tagArray) { foreach ($tagArray as $tag) { echo "#$tag "; } } function getAllTags($array) { $tagList = array(); foreach ($array as $entry) { $entryTags = splitTags($entry['tags']); foreach ($entryTags as $tag) { $key = array_search($tag, array_column($tagList, 'name')); if ($key == false) { $tagList[] = array( "name" => $tag, "count" => 1 ); } else { $tagList[$key]['count'] += 1; } } } array_multisort(array_column($tagList,'name'), SORT_ASC, SORT_NATURAL, $tagList); return $tagList; } function printAllTags($tagArray) { foreach ($tagArray as $tag) { $name = $tag['name']; $count = $tag['count']; echo "#$name ($count) "; } } function filterAlbums($albums, $tag) { $filteredAlbums = array(); foreach ($albums as $album) { $albumTags = splitTags($album['tags']); if (in_array($tag, $albumTags)) { $filteredAlbums[] = $album; } } return $filteredAlbums; } function printAlbums($albums) { foreach ($albums as $album) { $title = $album['title']; $artist = $album['artist']; $cover = $album['cover']; $tags = splitTags($album['tags']); include "albumtemplate.php"; } } ?> PHP filtering example

Basic PHP filtering example

Filter albums by tag:
View all

There are no albums with this tag.

"; } else { $taggedAlbums = filterAlbums($albums, $tag); echo "
"; printAlbums($taggedAlbums); echo "
"; } } else { echo "
"; printAlbums($albums); echo "
"; } ?>