简体   繁体   English

PHP:按子 arrays 的出现次数对多维数组进行排序

[英]PHP: sorting a multidimensional array by number of occurrences of the sub arrays

I want to sort a multidimensional array by appearances of the number of subarrays.我想按子数组的数量对多维数组进行排序。

This is my multidimensional array (it could hold any subarrays holding any values randomly ordered, this is just for testing purposes):这是我的多维数组(它可以包含任何包含随机排序的任何值的子数组,这仅用于测试目的):

$items = array (
array ("00008", "Metal", "Melvins", "Working With God", "Sub Pop", "SP 009"), 
array ("00019", "LP", "Ray Parker", "The Other Woman", "EMI", "EMI02"), 
array ("00019", "LP", "Ray Parker", "The Other Woman", "EMI", "EMI02"), 
array ("00019", "LP", "Ray Parker", "The Other Woman", "EMI", "EMI02"), 
array ("00021", "Techno", "Laurent Garnier", "Water Planet", "F Communications", "SDB00015"), 
array ("00056", "LP", "Communards", "Communards", "RCA", "E 342-F"), 
array ("00056", "LP", "Communards", "Communards", "RCA", "E 342-F"));

So that after sorting the multidimensional array becomes:这样排序后的多维数组就变成了:

$items = array (
array ("00019", "LP", "Ray Parker", "The Other Woman", "EMI", "EMI02"), 
array ("00019", "LP", "Ray Parker", "The Other Woman", "EMI", "EMI02"), 
array ("00019", "LP", "Ray Parker", "The Other Woman", "EMI", "EMI02"), 
array ("00056", "LP", "Communards", "Communards", "RCA", "E 342-F"), 
array ("00056", "LP", "Communards", "Communards", "RCA", "E 342-F"), 
array ("00008", "Metal", "Melvins", "Working With God", "Sub Pop", "SP 009"), 
array ("00021", "Techno", "Laurent Garnier", "Water Planet", "F Communications", "SDB00015"));

The purpose is that the subarray occuring three times appears first, followed by the subarray appearing two times.目的是先出现3次的子数组,再出现2次的子数组。 All the subarrays occurring one time can appear in random order.一次出现的所有子数组都可以随机出现。

Can it be done using a one-liner?可以使用单线完成吗?

For example: sorting out the uniques can be done like this:例如:排序唯一性可以这样完成:

$uniques = array_intersect_key ($items, array_unique (array_map ("serialize", $items)));

Many thanks for helping me out!非常感谢您帮助我!

This seems to be what you are looking for.这似乎是您正在寻找的。 As for a one-liner, not sure.至于单线,不确定。

// Grab the first column of each sub array, and then create an array
// mapping that ID to the number of times it was found in the main array
$counts = array_count_values(array_column($items, 0));

// Sort in reverse by value so that the greater quantity is first
arsort($counts);

// Custom sort where we look up the first column in our `$count` array
// and perform a spaceship comparison, going $b to $a so that the
// greater counts come first again.
usort(
    $items,
    static fn($a, $b) => $counts[$b[0]] <=> $counts[$a[0]]
);

Demo here: https://3v4l.org/Lm7L9此处演示: https://3v4l.org/Lm7L9

I should say, I'm not necessarily advocating for this code, it feels pretty cryptic, but it appears to work.我应该说,我不一定提倡这种代码,它感觉很神秘,但它似乎有效。 The fact that there's more comment than code is a code-small to me.评论多于代码的事实对我来说是一个小代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM