2013-06-05 25 views
1

正如你所看到的二維數組是如下:排序問題,並用在array_multisort()數組的大小警告不一致

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] =>)) 

我將嘗試用下面的代碼第3列進行排序:

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

然而,當IM印刷:

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 

我不知道爲什麼會這樣,它沒有任何意義,我,一切不過是我的工作需要這些警告了!

回答

0

這是一個警告,因爲在索引27處存在對您的數組的未定義引用。警告是說你正在嘗試使用該值,但它不存在。

再看看你的陣列

+0

該數組已排序並按要求打印!這可能是因爲數組$ sort_column不是二維數組! –

0

在array_multisort()如果用於許多數組進行排序一次呼應警告,如果所有選擇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():數組的大小是不一致的(... )

0

當兩個不相關的多維數組中使用了相同的鍵名或名稱時,會發生引發「數組大小不一致」的另一個條件。如果在設置第二個多維數組中的列之前清除密鑰名稱,則仍然可以使用相同的密鑰名稱。如果不鼓勵這樣做,我會有興趣知道爲什麼。