<?php $movies = [ [ 'title' => 'Snitch', 'genre' => 'Action' ], [ 'title' => 'Snitch', 'genre' => 'Drama' ], [ 'title' => 'Snitch', 'genre' => 'Crime' ], [ 'title' => 'Layer Cake', 'genre' => 'Crime' ], [ 'title' => 'Layer Cake', 'genre' => 'Drama' ], [ 'title' => 'Lock, stock and two smoking barrels', 'genre' => 'Crime' ], ]; $movie_genres = []; while ($row = next($movies)) { if (!isset($movie_genres[$row['title']])) { $movie_genres[$row['title']] = []; } $movie_genres[$row['title']][] = $row['genre']; } foreach ($movie_genres as $title => $genres) { echo $title . ': ' . implode(', ', $genres) .PHP_EOL; }
You have javascript disabled. You will not be able to edit any code.