用模数排序

Ech*_*ica 3 php sorting matrix modulus

我正在尝试使用uksort将列表排序为列.

数组已经进行了alpha排序,所以它就像 array('A','B','C','D','E','F','G','H','I','J','K','L','M')

它以html形式显示为浮动元素:

A B C D
E F G H
I J K L
M
Run Code Online (Sandbox Code Playgroud)

我希望它重新排序,所以它显示如下:

A E H K
B F I L
C G J M
D
Run Code Online (Sandbox Code Playgroud)

所以排序的数组将是: array('A','E','H','K','B','F','I','L','C','G','J','M','D'

基本上,与使用模数按字母顺序排序列表相同,但对于php.我已经尝试将javascript的解决方案转换为php,但我没有得到正确的答案.任何人有任何想法如何在PHP中这样做?

这是我尝试过的:

function cmp_nav_by4($a, $b) {
    if (($a % 5) < ($b % 5)) {
        return 1;
    } elseif (($a % 4) > ($b % 4)) {
        return -1;
    } else {
        return $a < $b ? 1 : -1;
    }
}
$result = uksort($thearray, "cmp_nav_by4");
Run Code Online (Sandbox Code Playgroud)

hak*_*kre 6

设置以下内容:

$array = range('A', 'M');
$columns = 4;
$length = count($array);

print_matrix($array, $columns);
Run Code Online (Sandbox Code Playgroud)

它通过索引(行和列)输出每个成员及其键,以及顶部的元素顺序:

One row - A B C D E F G H I J K L M
A[ 0] B[ 1] C[ 2] D[ 3] 
E[ 4] F[ 5] G[ 6] H[ 7] 
I[ 8] J[ 9] K[10] L[11] 
M[12] 
Run Code Online (Sandbox Code Playgroud)

链接的javascript代码可以很容易地转换为PHP.但是,如果仔细查看该问题/答案,很明显它只适用于完整行,就像我以前的尝试一样:

function callback_sort($array, $columns)
{
    $sort = function($columns)
    {
        return function($a, $b) use ($columns)
        {
            $bycol = ($a % $columns) - ($b % $columns);
            return $bycol ? : $a - $b;
        };
    };

    uksort($array, $sort(4));

    return $array;
}
Run Code Online (Sandbox Code Playgroud)

输出:

One row - A E I M B F J C G K D H L
A[ 0] E[ 4] I[ 8] M[12] 
B[ 1] F[ 5] J[ 9] C[ 2] 
G[ 6] K[10] D[ 3] H[ 7] 
L[11] 
Run Code Online (Sandbox Code Playgroud)

所以只是在另一个问题中提供的功能不起作用.

但是由于数组已经排序,您不需要再次对其进行排序,只是为了更改顺序或元素.但是哪个订单呢?如果矩阵不完整,例如n x n完全填充,则每列需要计算不同的新索引.采用13个元素(A-M)的示例为每列提供以下行分布:

column: 1 2 3 4
rows:   4 3 3 3
Run Code Online (Sandbox Code Playgroud)

因此,每列,值都不同.例如,在索引12处,第13个元素位于第4行.在前往该位置的路上,它已经通过第1列4次,在其他列2-4中通过3次.因此,要获得迭代索引的虚拟索引,您需要总结每列中的频率,以找出原始索引中有多少数字.如果超过最大成员数,则继续为0.

因此,可以通过逐步转发每个索引来迭代解决,以便在索引上分配计算:

Index 0:
    No column: 0

Index 1:
    1x in column is which has 4 rows: 4

Index 2:
    1x in column 1 (4 rows) and 1x in other columns (3 rows): 4 + 3
Run Code Online (Sandbox Code Playgroud)

... 等等.如果虚拟索引超过12,它将从0开始,例如对于虚拟索引将计算的第5个元素(索引4)13:

Index 4:
    1x 4 rows and 3x 3 rows = 13 (4 + 9)
    13 > 12 => 1 (13 - 12)
Run Code Online (Sandbox Code Playgroud)

现在通过从虚拟索引开始0并每次给出适当的偏移量来填充一个新数组(查看您所在的列,添加该列的行数,必要时换行)将提供所需的输出:

One row - A E H K B F I L C G J M D
A[ 0] E[ 4] H[ 7] K[10] 
B[ 1] F[ 5] I[ 8] L[11] 
C[ 2] G[ 6] J[ 9] M[12] 
D[ 3] 
Run Code Online (Sandbox Code Playgroud)

用代码编写,这foreach比原始索引简单.通过维护键的索引,这适用于任何数组,甚至包含字符串键的数组:

$floor = floor($length/$columns);
$modulo = $length % $columns;
$max = $length-1;
$virtual = 0;
$keys = array_keys($array);
$build = array();
foreach($keys as $index => $key)
{
    $vkey = $keys[$virtual];
    $build[$vkey] = $array[$vkey];
    $virtual += $floor + ($index % $columns < $modulo);
    ($virtual>$max) && $virtual %= $max;
}

print_matrix($build, $columns);
Run Code Online (Sandbox Code Playgroud)

就是这样:演示,Gist.