2014-11-06 106 views
0

我有一個多維數組。現在我需要按照一個值的順序對這個數組進行排序。使用值排序多維數組

下面是這個數組的print_r的:

Array 
(
    [2] => Array 
     (
      [NoticeType] => Invoice or Statement 
      [PhoneNumber] => 2222222222 
      [NoticeIdentifier] => Firm1 
      [NoticeDescription] => test 
      [AdditionalComments] => test1 
      [LineItemGHN] => Monthly Charges 
      [Amount] => 100 
      [StartDate] => 21/10/2014 
      [EndDate] => 
      [TaxGroup] => GP-1 (GP-1. Service Tax (12) + Edu Cess (0.24) + Sec & High Edu Cess (0.12) for a total of 12.36%) 

     ) 

    [3] => Array 
     (
      [NoticeType] => Notice 
      [PhoneNumber] => 3333333333 
      [NoticeIdentifier] => Firm1 
      [NoticeDescription] => test2 
      [AdditionalComments] => test2 
      [LineItemGHN] => Half Yearly Subscription 
      [Amount] => 200 
      [StartDate] => 21/10/2014 
      [EndDate] => 
      [TaxGroup] => GP-0 (No Taxes) 
     ) 

    [4] => Array 
     (
      [NoticeType] => Invoice or Statement 
      [PhoneNumber] => 2222222222 
      [NoticeIdentifier] => Firm1 
      [NoticeDescription] => test 
      [AdditionalComments] => test1 
      [LineItemGHN] => Discounts 
      [Amount] => 50 
      [StartDate] => 21/10/2014 
      [EndDate] => 
      [TaxGroup] => GP-1 (GP-1. Service Tax (12) + Edu Cess (0.24) + Sec & High Edu Cess (0.12) for a total of 12.36%) 
     ) 

    [5] => Array 
     (
      [NoticeType] => Invoice or Statement 
      [PhoneNumber] => 2222222222 
      [NoticeIdentifier] => Firm2 
      [NoticeDescription] => test 
      [AdditionalComments] => test1 
      [LineItemGHN] => Monthly Charges 
      [Amount] => 2500 
      [StartDate] => 21/10/2014 
      [EndDate] => 
      [TaxGroup] => GP-1 (GP-1. Service Tax (12) + Edu Cess (0.24) + Sec & High Edu Cess (0.12) for a total of 12.36%) 
     ) 

我需要排序這個數組根據PhoneNumber .I'm從去年4個小時艱苦努力我的運氣。如何做那個排序?

+0

給代碼,你試過 – 2014-11-06 09:08:41

+0

http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value檢查此鏈接。 – Piyush 2014-11-06 09:10:55

+0

@suby 我在'$ var'中得到這個數組。所以這是我試過 'foreach($ var as $ key => $ val){ \t \t $ PhoneNumber = $ val ['PhoneNumber']; \t \t \t $ NoticeIdentifier = $ val ['NoticeIdentifier']; \t \t \t $ StartDate = $ val ['StartDate']; \t \t \t $ newAr [$ NoticeIdentifier] [$ StartDate] [$ PhoneNumber] [] = $ val; \t}' – Kanav 2014-11-06 10:16:15

回答

0

使用usort時,這將調用一個函數,該函數在比較數組中的兩個元素時應該返回低於0,0或更大的值。

function myPhonenumberSort($a,$b){//$a and $b are two array elements that needs to be compared 
    if($a['PhoneNumber']==$b['PhoneNumber')return 0; //Return 0 because they are the same. 
    return ($a['PhoneNumber']<$b['PhoneNumber'])?-1:1; // A is smaller, so return -1, else return 1 (b is bigger) 
} 
usort($myArray,'myPhonenumberSort'); 

欲瞭解更多信息,請閱讀PHP文檔在http://php.net/manual/en/function.usort.php

編輯:請注意,在這種非常簡單的對比,可以簡單地

return $a['PhoneNumber']-$b['PhoneNumber'];

0

要做到這一點,您可以使用功能usort並將其傳遞給您的比較功能。例如:

function cmp($a, $b) 
{ 
    if ($a["PhoneNumber"] == $a["PhoneNumber"]) { 
     return 0; 
    } 
    return ($a["PhoneNumber"] < $a["PhoneNumber"]) ? -1 : 1; 
} 

usort($yourArray, "cmp"); 
0

使用usort會對此進行排序。

function cmp($a, $b) { 
     return $a["PhoneNumber"] - $b["PhoneNumber"]; 
} 
usort($arr, "cmp");