基于另一个阵列的订单排序阵列子项 [英] Sort Array Subkey Based on Another Array's Order

查看:153
本文介绍了基于另一个阵列的订单排序阵列子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有关于数组排序的很多帖子,但我已经看过高低并不能找到一个解决我的问题。

我发现了一个很不错的文章在这里:
http://firsttube.com/read/sorting-a-多维数组与 - PHP /

上面的链接是伟大的,但是,我要采取进一步和排序数组子项基于了另一个数组的顺序。

所以,让我们说我们有五首歌曲的播放列表数组:

 阵列

    [0] => 3OH 3 - 不要相信我
    [1] =>泰勒·斯威夫特 - 你是属于我
    [2] =>肖恩金斯顿 - 火燃烧
    [3] =>绿日 - 了解你的敌人
    [4] =>凯利克拉克森 - 飘

并让我们说我们有我们想进行排序,以配合我们的播放列表数组的顺序如下信息:

 阵列

    [0] =>排列
        (
            [TRACKNAME] =>泰勒·斯威夫特 - 你是属于我
            [trackLength] => 0
            [trackViews] => 0
            [trackRating] => 0
        )    [1] =>排列
        (
            [TRACKNAME] =>肖恩金斯顿 - 火燃烧
            [trackLength] => 0
            [trackViews] => 0
            [trackRating] => 0
        )    [2] =>排列
        (
            [TRACKNAME] => 3OH!3不要信任我
            [trackLength] => 205
            [trackViews] => 4570399
            [trackRating] => 4.866372
        )    [3] =>排列
        (
            [TRACKNAME] => Green Day的了解你的敌人
            [trackLength] => 191
            [trackViews] => 4715494
            [trackRating] => 4.9103785
        )    [4] =>排列
        (
            [TRACKNAME] =>凯莉·克拉克森:飘
            [trackLength] => 225
            [trackViews] => 679019
            [trackRating] => 4.8995433
        ))

所以,一次...我的问题是:我如何能获得歌曲阵列由TRACKNAME进行排序,以匹配播放列表阵列相同的顺序

任何帮助或指导,将AP preciated!

感谢。

[解决]
EDDIE有高手成功的解决方案!道具给他! 1UP他的答案! :)

不过,我稍微修改埃迪的解决方案。我刚才添加的用户输入歌曲名称和歌曲标题之间75%的相似支票抬头的外部来源,因为我无法控制每个标题的细微变化。

  $ sorted_list =阵列();的foreach($歌曲$ song_key => $歌曲){
   的foreach($ song_info为$资讯){
      similar_text($信息['TRACKNAME'],$歌曲,$ P);
      如果($ P> 75){
        $ sorted_list [$ song_key] = $信息;
      }
   }
}


解决方案

假设你的播放列表是数组$歌曲和你的另一阵列是$ songs_info。

  $ sorted_list =阵列();的foreach($歌曲$ song_key => $歌曲){
   的foreach($ song_info为$资讯){
      如果($信息['TRACKNAME'] == $歌曲){
          $ sorted_list [$ song_key] = $信息;
      }
   }
}// $ sorted_list包含排序列表

I know there have been many posts regarding array sorting, but I have looked high and low and cannot find a solution to my problem.

I have found a very good article here: http://firsttube.com/read/sorting-a-multi-dimensional-array-with-php/

The above link is great, however, I wish to take it further and sort the array subkey based off the order of another array.

So, let's say we have five songs in the Playlist Array:

Array
(
    [0] => 3oh!3 - Don't Trust me
    [1] => Taylor Swift - You Belong with me
    [2] => Sean Kingston - Fire Burning
    [3] => Green Day - Know Your Enemy
    [4] => Kelly Clarkson - Gone
)

And let's say we have the following information that we would like sorted to match the order of our Playlist Array:

Array
(
    [0] => Array
        (
            [trackName] => Taylor Swift - You Belong With Me
            [trackLength] => 0
            [trackViews] => 0
            [trackRating] => 0
        )

    [1] => Array
        (
            [trackName] => Sean Kingston - Fire Burning
            [trackLength] => 0
            [trackViews] => 0
            [trackRating] => 0
        )

    [2] => Array
        (
            [trackName] => 3OH!3- Dont Trust Me 
            [trackLength] => 205
            [trackViews] => 4570399
            [trackRating] => 4.866372
        )

    [3] => Array
        (
            [trackName] => Green Day Know Your Enemy 
            [trackLength] => 191
            [trackViews] => 4715494
            [trackRating] => 4.9103785
        )

    [4] => Array
        (
            [trackName] => Kelly Clarkson: Gone 
            [trackLength] => 225
            [trackViews] => 679019
            [trackRating] => 4.8995433
        )

)

So, again... My question is: How can I get the array of songs to be sorted by the trackName to match the same order of the Playlist Array?

Any help or guidance would be appreciated!

Thanks.

[SOLUTION] EDDIE had the master winning solution! Props to him! 1up his answer! :)

However, I modified eddie's solution slightly. I just added a similarity check of 75% between the user entered song titles and the song titles looked up on the external sources because I cannot control the slight variations of each title.

$sorted_list = array();

foreach($songs as $song_key=>$song){
   foreach($song_info as $info){
      similar_text($info['trackName'], $song, $p);
      if($p > 75){ 
        $sorted_list[$song_key] = $info;
      }
   }
}

解决方案

Assuming your playlist is in the array $songs and your other array is in $songs_info.

$sorted_list = array();

foreach($songs as $song_key=>$song){
   foreach($song_info as $info){
      if($info['trackName'] == $song){
          $sorted_list[$song_key] = $info;
      }
   }
}

// $sorted_list contains sorted list

这篇关于基于另一个阵列的订单排序阵列子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆