php - 对 Mysql 数组进行切片

标签 php mysql

我有一个 mysql 查询

$raw_resultsplaylists = mysql_query("SELECT songs.*, playlists.title as `playlist_title`
                                 FROM song_main AS songs
                                 JOIN songs_in_list AS playlist_songs 
                                  ON songs.ID = playlist_songs.song_id
                                 JOIN playlists 
                                 ON playlist_songs.playlist_id = playlists.ID
                                WHERE playlists.owner_id = '$userid'") or die(mysql_error());

检索如下信息的数组:

  playlist_title      song_name              song_url        song_artist
     -------------------------------------------------------------------        
     foo1               woops                 www.                -
     foo1               blah                  www.                -
     foo2               blop                  www.                -
     foo1               woop                  www.                -
     foo3               bob                   www.                -      
     foo1               dylan                 www.                -

我的问题是如何对 Mysql 数组进行切片,以在单独的数组中获取具有相同 playlist_title 的所有行,以便我可以在单独的 html 表中显示它们:

html 表 1:

 playlist_title      song_name              song_url        song_artist
    -------------------------------------------------------------------        
     foo1               woops                 www.                -
     foo1               blah                  www.                -
     foo1               woop                  www.                -    
     foo1               dylan                 www.                -

html 表 2

    playlist_title      song_name           song_url        song_artist
     -------------------------------------------------------------------        
     foo2               blop                  www.                -
     foo2               dylan                 www.                -

等等...

最佳答案

只是为了画出这个想法..

do{

  $groupedItems[$row['pname']][] = $row; //every row with the same pname will be "grouped" in the same array level

}while..

然后...

foreach($groupedItems as $group){
  //bla bla
}

关于php - 对 Mysql 数组进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993319/

相关文章:

php session 变量在下一页丢失

php - 读取包含文件会减慢 php 脚本加载速度吗?

MySQL 将自定义格式的日期与现在进行比较

mysql - SQL 连接和选择

mysql不在时间和日期之间

mysql - 如何在不删除现有数据的情况下更新 MySQL 表列

php - 具有相同重写 URL 的 2 个不同页面的 .htaccess 规则

php - 创建干净的 HABTM 查询

php - 从 PHP 调用 gzip 失败

php - 使用:label for SQL UPDATE statement?有什么问题