array_multisort():数组大小不一致

Posted

技术标签:

【中文标题】array_multisort():数组大小不一致【英文标题】:array_multisort(): Array sizes are inconsistent 【发布时间】:2014-04-10 11:13:32 【问题描述】:

我正在尝试使用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 ) 
)

【问题讨论】:

检查array_multisort 的工作方式。第三个例子对你有帮助:array_multisort() 需要一个列数组 对此发表评论。我在使用 php 5.x 时遇到了同样的问题,当更改为 PHP 7.x 解决了这个问题。 【参考方案1】:

下面一行有错误:

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

您正试图将返回结果存储到一个数组中,但 array_multisort 返回的是布尔值而不是排序后的数组:

对多维数组进行排序$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 数组现在按上述操作排序..

【讨论】:

【参考方案2】:

为了能够使用 array_multisort,您应该重新组织您的数组。 请参阅此处的示例 #3:http://uk1.php.net/array_multisort

或者您可以使用 usort,但它会重新编号键:

<?php
$mar =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' ) ,
);

usort($mar, function($a,$b)return $a['wek']-$b['wek'];);
print_r($mar);

【讨论】:

以上是关于array_multisort():数组大小不一致的主要内容,如果未能解决你的问题,请参考以下文章

PHP数组排序函数array_multisort()函数详解

PHP数组排序函数array_multisort()函数详解

PHP array_multisort—对多个数组或多维数组进行排序

实现对多维数组按照某个键值排序的两种方法(array_multisort和array_sort)

二维,多维数组排序array_multisort()函数的使用

数组排序array_multisort