多维度数组排序思路

/**
* Filename: array_sort.php.
* User: George
* Date: 2017/11/10
* Time: 下午3:42
*/

$arr1 = array (
0 => ’1_93′,
1 => ’2_46′,
2 => ’1_151′,
);

$arr2 = array (
0 =>
array (
‘document_id’ => ’93′,
‘id’ => ‘b2e62e0214684938fe6ee9827ccdc649′,
‘title’ => ‘添加文章1′,
‘tags’ => ’10,75′,
‘score’ => 1001,
‘description’ => ‘添加文章’,
‘cover_pic_url’ => ‘http://bootfirst-oss02.img-cn-beijing.aliyuncs.com/tmp_cover_pics/73/53/5d/f3/73535df3b7200dddb17fd399ea01da3e.jpg’,
‘price’ => 0,
‘level’ => 0,
‘type’ => 1,
‘url’ => ‘http://m-shiting.office.tengyue360.com/content/document/article?id=b2e62e0214684938fe6ee9827ccdc649′,
‘is_series’ => false,
‘create_ts’ => 1468576809,
‘is_new’ => false,
‘view_count’ => 2644,
‘share_count’ => 0,
‘fav_count’ => 1,
‘comment_count’ => 97,
‘recommend_type’ => ‘manual’,
‘unique_key’ => ’1_93′,
‘unique_id’ => ’112′,
‘ordinal’ => ’201711102′,
),
1 =>
array (
‘document_id’ => ’151′,
‘id’ => ’1cb1e65e8cf5e1a8537a1750c4cdb223′,
‘title’ => ‘实战经验 培训机构新老师培养秘籍’,
‘tags’ => ’3,9,10,11,12,109,18,122,38′,
‘score’ => 100,
‘description’ => ‘各位校长大家好,我是腾飞教育英语教研组长卢宇徽。今天和大家分享的主题是“新老师的培养”。’,
‘cover_pic_url’ => ”,
‘price’ => 0,
‘level’ => 0,
‘type’ => 1,
‘url’ => ‘http://m-shiting.office.tengyue360.com/content/document/article?id=1cb1e65e8cf5e1a8537a1750c4cdb223′,
‘is_series’ => true,
‘create_ts’ => 1468916705,
‘is_new’ => false,
‘view_count’ => 623,
‘share_count’ => 4,
‘fav_count’ => 6,
‘comment_count’ => 14,
‘recommend_type’ => ‘manual’,
‘unique_key’ => ’1_151′,
‘unique_id’ => ’88′,
‘ordinal’ => ’0′,
),
2 =>
array (
‘id’ => ‘b88541b279e17c24f4d6eb03512a40aa’,
‘title’ => ’11′,
‘author_name’ => ’1′,
‘author_title’ => ’1′,
‘author_photo’ => ‘http://bootfirst-oss02.img-cn-beijing.aliyuncs.com/dailyaudio_author_photo/a9/96/87/7e/a996877e252644424015eec4c9ade991.jpg’,
‘duration’ => 1,
‘outer_url’ => ‘http://bootfirst-oss02.oss-cn-beijing.aliyuncs.com/dailyaudio/bb99d7a81e665b089f926f517c0dcbe2.jpg’,
‘content_url’ => ‘http://m-shiting.office.tengyue360.com/content/document/dailyaudiocontent?id=b88541b279e17c24f4d6eb03512a40aa’,
‘view_count’ => 15,
‘fav_count’ => 0,
‘share_count’ => 0,
‘publish_ts’ => 1501948800,
‘unique_key’ => ’2_46′,
‘type’ => 4,
‘unique_id’ => ’89′,
‘ordinal’ => ’0′,
‘cover_pic_url’ => ‘http://bootfirst-oss02.oss-cn-beijing.aliyuncs.com/dailyaudio/bb99d7a81e665b089f926f517c0dcbe2.jpg’,
‘url’ => ‘http://m-shiting.office.tengyue360.com/content/document/dailyaudiocontent?id=b88541b279e17c24f4d6eb03512a40aa’,
),
);

usort($arr2, function($a, $b) use ($arr1) {
$pos_a = array_search($a['unique_key'], $arr1);
$pos_b = array_search($b['unique_key'], $arr1);
if($pos_a == $pos_b) {
return 0;
}
return ($pos_a < $pos_b) ? -1 : 1;
});

print_r($arr2);

此条目发表在 涨姿势 分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*


*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>