按键值对php中的关联数组进行排序

时间:2016-02-09 09:40:03

标签: php arrays sorting

我的数组看起来像这样

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

我想根据rowcolumn键的值按升序对此数组进行排序。 这是预期的结果。

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

即当我在排序的关联数组中打印rowcolumn键的值时 它看起来应该是这样的。

 00
 02
 12

那么我该如何排序呢?

1 个答案:

答案 0 :(得分:1)

为了便于阅读,您可能需要稍微清理一下,功能有点乱,但它应该完成工作。任何问题都让我知道。

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

使用示例:http://codepad.org/wyyqJCwg