使用值对多维数组进行排序

时间:2014-11-06 09:06:30

标签: php arrays sorting

我有一个多维数组。现在我需要按一个值的顺序对这个数组进行排序。

这是这个数组的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对这个数组进行排序。我在过去4小时内努力运气。怎么做那个排序?

3 个答案:

答案 0 :(得分: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');

有关详细信息,请阅读http://php.net/manual/en/function.usort.php

上的PHP文档

编辑:请注意,在这种非常简单的比较中,可以简单地

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

答案 1 :(得分:0)

为此,您可以使用函数usort并将其传递给您的比较函数。例如:

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

usort($yourArray, "cmp");

答案 2 :(得分:0)

使用usort会对此进行排序。

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