2

I have an array look like this

Array
(
    [0] => Array
        (
            [width] => 1
            [ladiesSeat] => 
            [fare] => 610
            [zIndex] => 0
            [serviceTaxAmount] => 35.38
            [commission] => 
            [operatorServiceChargeAbsolute] => 0
            [operatorServiceChargePercent] => 0
            [totalFareWithTaxes] => 645.38
            [bookedBy] => 
            [ac] => 
            [sleeper] => 
            [serviceTaxPer] => 5.8
            [available] => 1
            [row] => 0
            [column] => 2
            [length] => 1
            [id] => M1
        )

    [1] => Array
        (
            [width] => 1
            [ladiesSeat] => 
            [fare] => 610
            [zIndex] => 0
            [serviceTaxAmount] => 35.38
            [commission] => 
            [operatorServiceChargeAbsolute] => 0
            [operatorServiceChargePercent] => 0
            [totalFareWithTaxes] => 645.38
            [bookedBy] => 
            [ac] => 
            [sleeper] => 
            [serviceTaxPer] => 5.8
            [available] => 1
            [row] => 1
            [column] => 0
            [length] => 1
            [id] => L21
        )
      ...............
      ...............
     [n] => Array
        (
            [width] => 1
            [ladiesSeat] => 
            [fare] => 610
            [zIndex] => 0
            [serviceTaxAmount] => 35.38
            [commission] => 
            [operatorServiceChargeAbsolute] => 0
            [operatorServiceChargePercent] => 0
            [totalFareWithTaxes] => 645.38
            [bookedBy] => 
            [ac] => 
            [sleeper] => 
            [serviceTaxPer] => 5.8
            [available] => 1
            [row] => 0
            [column] => 0
            [length] => 1
            [id] => L21
        )
)

I want to sort this array based on the values of row and column keys in ascending order. Here is the expected result.

Array
    (
        [0] => Array
            (
                [width] => 1
                [ladiesSeat] => 
                [fare] => 610
                [zIndex] => 0
                [serviceTaxAmount] => 35.38
                [commission] => 
                [operatorServiceChargeAbsolute] => 0
                [operatorServiceChargePercent] => 0
                [totalFareWithTaxes] => 645.38
                [bookedBy] => 
                [ac] => 
                [sleeper] => 
                [serviceTaxPer] => 5.8
                [available] => 1
                [row] => 0
                [column] => 0
                [length] => 1
                [id] => M1
            )

        [1] => Array
            (
                [width] => 1
                [ladiesSeat] => 
                [fare] => 610
                [zIndex] => 0
                [serviceTaxAmount] => 35.38
                [commission] => 
                [operatorServiceChargeAbsolute] => 0
                [operatorServiceChargePercent] => 0
                [totalFareWithTaxes] => 645.38
                [bookedBy] => 
                [ac] => 
                [sleeper] => 
                [serviceTaxPer] => 5.8
                [available] => 1
                [row] => 0
                [column] => 2
                [length] => 1
                [id] => L21
            )
          ...............
          ...............
         [n] => Array
            (
                [width] => 1
                [ladiesSeat] => 
                [fare] => 610
                [zIndex] => 0
                [serviceTaxAmount] => 35.38
                [commission] => 
                [operatorServiceChargeAbsolute] => 0
                [operatorServiceChargePercent] => 0
                [totalFareWithTaxes] => 645.38
                [bookedBy] => 
                [ac] => 
                [sleeper] => 
                [serviceTaxPer] => 5.8
                [available] => 1
                [row] => 1
                [column] => 2
                [length] => 1
                [id] => L21
            )
    )

ie when i print values of row and column key in the sorted associated array it should look like this way.

 00
 02
 12

So how can i sort it?

Blessan Kurien
  • 1,645
  • 9
  • 31
  • 63

1 Answers1

1

You may want to clean this up a little for readability sake, the function is a little messy but it should get the job done. Any problems just let me know.

function rowColumnSort($a, $b) {
    // Row is lower.
    if ($a['row'] < $b['row']) {
        return -1;
    }

    // Rows Match, check columns
    if ($a['row'] == $b['row']) {
        // Column Higher
        if ($a['column'] > $b['column']) {
            return 1;   
        } else {
            // Column lower
            return -1;
        }
    }

    // Row is higher
    if ($a['row'] > $b['row']) {
        return 1;
    }
}

usort($arr, 'rowColumnSort');

Example Usage: http://codepad.org/wyyqJCwg

Mikey
  • 2,606
  • 1
  • 12
  • 20