0

I have a multi-dimensional array. Now i need to sort this array in the order of one value.

Here's the print_r of this array:

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%)
        )

I need to sort this array according to PhoneNumber.I'm trying my luck hard from last 4 hrs. How to do that sorting?

Kanav
  • 2,695
  • 8
  • 34
  • 56
  • http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value Check this link. – Piyush Nov 06 '14 at 09:10
  • @suby I was getting this array in `$var`. So this is what i tried `foreach($var as $key => $val){ $PhoneNumber = $val['PhoneNumber']; $NoticeIdentifier = $val['NoticeIdentifier']; $StartDate = $val['StartDate']; $newAr[$NoticeIdentifier][$StartDate][$PhoneNumber][] = $val; }` – Kanav Nov 06 '14 at 10:16

3 Answers3

0

Use usort, this calls a function which should return lower than 0, 0, or greater when comparing two elements from the array.

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');

For more info, read the PHP docs at http://php.net/manual/en/function.usort.php

EDIT: note that on this kind of very simple comparison, it is possible to simply

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

Salketer
  • 14,263
  • 2
  • 30
  • 58
0

To do this, you can use the function usort and passing it your comparing function. For instance:

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

usort($yourArray, "cmp");
mgaido
  • 2,987
  • 3
  • 17
  • 39
0

Use usort will sort this.

function cmp($a, $b) {
        return $a["PhoneNumber"] - $b["PhoneNumber"];
}
usort($arr, "cmp");
Isuru Madusanka
  • 356
  • 3
  • 13