1

如您所见,二维数组如下:

Array ( [0] => Array ( [0] => Prishtina [1] => Bregu i Diellit [2] => 7 ) 
    [1] => Array ( [0] => Prishtina [1] => Tasligje [2] => 4 ) 
    [2] => Array ( [0] => Prishtina [1] => Bregu i Diellit [2] => 4 ) 
    [3] => Array ( [0] => Prishtina [1] => Bregu i Diellit [2] => 3 ) 
    [4] => Array ( [0] => Prishtina [1] => Bregu i Diellit [2] => 9 ) 
    [5] => Array ( [0] => Prishtina [1] => Lakrishte [2] => 4 ) 
    [6] => Array ( [0] => Prishtina [1] => Lakrishte [2] => 6 ) 
    [7] => Array ( [0] => Prishtina [1] => Lakrishte [2] => 1 ) 
    [8] => Array ( [0] => Prishtina [1] => Ulpiana [2] => 3 ) 
    [9] => Array ( [0] => Prishtina [1] => Ulpiana [2] => 6 ) 
    [10] => Array ( [0] => Prishtina [1] => Ulpiana [2] => 5 ) 
    [11] => Array ( [0] => Prishtina [1] => Ulpiana [2] => 8 ) 
    [12] => Array ( [0] => Prishtina [1] => Ulpiana [2] => 4 ) 
    [13] => Array ( [0] => Prishtina [1] => Ulpiana [2] => 8 ) 
    [14] => Array ( [0] => Prishtina [1] => Velania [2] => 2 ) 
    [15] => Array ( [0] => Prishtina [1] => Velania [2] => 5 ) 
    [16] => Array ( [0] => Prishtina [1] => Velania [2] => 1 ) 
    [17] => Array ( [0] => Prishtina [1] => Dragodan [2] => 9 ) 
    [18] => Array ( [0] => Prishtina [1] => Dragodan [2] => 7 ) 
    [19] => Array ( [0] => Prishtina [1] => Dragodan [2] => 10 ) 
    [20] => Array ( [0] => Prishtina [1] => Velania [2] => 11 ) 
    [21] => Array ( [0] => Mitrovica [1] => Qender [2] => 5 ) 
    [22] => Array ( [0] => Mitrovica [1] => Qender [2] => 3 ) 
    [23] => Array ( [0] => Mitrovica [1] => Tavnik [2] => 6 ) 
    [24] => Array ( [0] => Mitrovica [1] => Tavnik [2] => 9 ) 
    [25] => Array ( [0] => Mitrovica [1] => Bair [2] => 10 ) 
    [26] => Array ( [0] => Mitrovica [1] => Bair [2] => 12 ) 
    [27] => Array ( [0] => ) ) 

我正在尝试使用以下代码按第三列排序:

foreach ($final_array as $roww){
    $sort_column[]= $roww[2];   // 2 = your example
    array_multisort($sort_column, $final_array);
}

但是,当我打印时:

for ($row = 0; $row < count($final_array); $row++)
{
    foreach($final_array[$row] as $key => $value)
        {
            echo $value;
        }
        echo "<br/>";
 }

当我打印时,它会排序并打印按第三列排序的数组,但是它给我带来了这些警告

Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in   C:\xampp\htdocs\test\test.php on line 35
 Notice: Undefined offset: 2 in C:\xampp\htdocs\test\test.php on line 34

我不确定为什么会发生这种情况,这对我来说没有意义,一切正常,但是我需要这些警告!

4

3 回答 3

0

array_multisort() 如果用于一次对多个数组进行排序,则会回显警告,如果选择执行多重排序的所有数组在大小上都不同:

$arr = array(
    'UK' => array(
        'John' => 'Toyota',
        'Alice' => 'Opel',
        'Evan' => 'Fiat',
        'Gregory' => 'Mitsubishi'
    ),
    'Germany' => array(
        'Bruno' => 'Opel',
        'Andreas' => 'Mercedes',
        'Klaus' => 'Porsche',
    )
);

看?两个数组中的元素数量不同。因此,array_multisort 将尝试以相同的方式对这些数组进行排序,即使选项不同:

array_multisort(
    $arr['Germany'], SORT_ASC, SORT_NATURAL,
    $arr['UK'], SORT_DESC, SORT_NUMERIC
    );

这将打印如下警告:警告:array_multisort():数组大小在 (...) 中不一致

于 2014-04-08T08:42:47.957 回答
0

这是一个警告,因为在 index 处存在对数组的未定义引用27。警告是说您正在尝试使用该值,但它不存在。

再看看你的阵列

于 2013-06-06T00:04:06.363 回答
0

当在两个不相关的多维数组中使用相同的键名或名称时,会引发“数组大小不一致”的另一种情况。如果在设置第二个多维数组中的列之前清除键名,您仍然可以使用相同的键名。如果不鼓励这样做,我很想知道为什么。

于 2018-03-03T20:27:18.430 回答