2012-03-26 96 views
0

排序多維數組我有這樣的誰擁有指數

Array 
(
    [0] => Array 
    (
     [UnitESN] => 14296 
     [ScanNo] => 1 
     [ScanDate] => Nov 21 
     [ScanTime] => 10:15 AM 
     [Qualifiers] => 
     [Notes] => 
     [BadgeData] => Array 
      (
       [0] => HEATH--- 
       [1] => MCCU---- 
       [2] => HER--- 
       [3] => [email protected] 
       [4] => 
       [5] => 393 
       [6] => 13350 
       [7] => 
       [8] => 
       [9] => 111 
      ) 

     [Signal] => +00/000 
     [ConnectionDelay] => 0407 
    ) 

    [1] => Array 

數組等等......我想訂購ASC或DESC ......讓我們在第8欄說和Col 8項數7 (8-1,因爲它從零開始)在BadgeData中,有什麼想法?我嘗試array_multisort但沒有成功。

感謝

+2

在我看來,你正在尋找['uasort'](http://ca2.php.net/uasort)。 – 2012-03-26 17:02:06

+0

[按照子值排序php多維數組]的可能重複(http://stackoverflow.com/questions/4508145/sort-php-multidimensional-array-by-sub-value) – Jon 2012-03-26 17:07:42

+0

如果你想要的信貸弗朗索瓦,post一個答案。我找到了! @Jon:在你的「重複」中不是正確的答案 – 2012-03-26 17:08:40

回答

1

我很高興你想通了。這是我在被打斷前開始寫的東西。

基本uasort例子:

<?php 

function cmp($a, $b) { 
    if ($a['BadgeData'][7] == $b['BadgeData'][7]) { 
     return 0; 
    } 
    // Ascending 
    return ($a['BadgeData'][7] < $b['BadgeData'][7]) ? -1 : 1; 
} 

// Order the values of the array based on the value in BadgeData[7] in ascending order.. 
uasort($array, 'cmp'); 

它看起來像我可能雖然誤解你原來的問題,我還以爲你要由值數組中BadgeData[7]排序,但好像你想整理每個數組值的BadgeData。

+0

我有一個數組[0] => [1] =>等...直到1000 ...每個數組都有一個名爲BadgeData的行。在該行內部,有一個名爲[0],[1],[2]等的數組,它可以是[2],有時它可以是[50000]取決於它是什麼...所以我需要以此來排序。 [1]可以持有姓名或電話,甚至地址,這就是爲什麼......但非常感謝,我標記你的答案。 – 2012-03-26 17:35:17

1

由於@Francois Deschenes引領我在正確的答案。這裏是我的發現:

http://ca2.php.net/manual/en/function.uasort.php#104714

我編輯,以適合我的需要。謝謝 !

function SortArrayByCol(array $Array, $Key, $ASC=true, $Col=0) { 
    $Result = array(); 

    $Values = array(); 
    foreach($Array as $ID => $Value){ 
     $Values[$ID] = isset($Value[$Key][$Col]) ? $Value[$Key][$Col] : null; 
    } 

    if($ASC){ 
     asort($Values); 
    } else { 
     arsort($Values); 
    } 

    foreach($Values as $Key => $Value) { 
     $Result[$Key] = $Array[$Key]; 
    } 

    return $Result; 
}