我正在尝试使用 array_multisort() 函数对其子数组的基础上的数组进行排序......

在尝试的同时;

print_r($mar); echo '<br>';
$arr2 = array_multisort($mar, array('wek'=>SORT_ASC));
print_r($arr2);

得到错误 array_multisort(): Array sizes are inconsistent
排序前的输出是
Array (
    [0] => Array ( [dat] => 1 [wek] => 5 [mac] => A100 [mcr] => #00c8ff )
    [1] => Array ( [dat] => 2 [wek] => 9 [mac] => A100 [mcr] => #00c8ff )
    [2] => Array ( [dat] => 5 [wek] => 13 [mac] => A100 [mcr] => #00c8ff )
    [3] => Array ( [dat] => 5 [wek] => 6 [mac] => A101 [mcr] => #ff8800 )
    [4] => Array ( [dat] => 13 [wek] => 17 [mac] => A100 [mcr] => #00c8ff )
    [5] => Array ( [dat] => 20 [wek] => 21 [mac] => A100 [mcr] => #00c8ff )
    [6] => Array ( [dat] => 8 [wek] => 14 [mac] => A101 [mcr] => #ff8800 )
)

我需要的是:
Array (
    [0] => Array ( [dat] => 1 [wek] => 5 [mac] => A100 [mcr] => #00c8ff )
    [3] => Array ( [dat] => 5 [wek] => 6 [mac] => A101 [mcr] => #ff8800 )
    [1] => Array ( [dat] => 2 [wek] => 9 [mac] => A100 [mcr] => #00c8ff )
    [2] => Array ( [dat] => 5 [wek] => 13 [mac] => A100 [mcr] => #00c8ff )
    [6] => Array ( [dat] => 8 [wek] => 14 [mac] => A101 [mcr] => #ff8800 )
    [4] => Array ( [dat] => 13 [wek] => 17 [mac] => A100 [mcr] => #00c8ff )
    [5] => Array ( [dat] => 20 [wek] => 21 [mac] => A100 [mcr] => #00c8ff )
)

最佳答案

下面一行有错误:

$arr2 = array_multisort($mar, array('wek'=>SORT_ASC));

您试图将返回结果存储到数组中,但 array_multisort 返回 bool 值而不是排序数组:

这样做是为了对多维数组 $mar 进行排序:
foreach ($mar as $key => $row)
{
    $wek[$key]  = $row['wek'];
}

// Sort the data with wek ascending order, add $mar as the last parameter, to sort by the common key

array_multisort($wek, SORT_ASC, $mar);

$mar 数组现在在上述操作后排序。

关于php - array_multisort() : Array sizes are inconsistent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22247844/

10-10 22:48