dpd3447
2014-12-03 21:30
浏览 33
已采纳

如何在PHP中的几个不同的键上对多维数组进行排序?

I am creating a multidimensional array from an array that is returned from the database. Then I need to sort the array that I have created on 3 different keys. So, here is my array.

foreach($priceList->data as $obj){
    $d1d2[] = $obj->d1d2desc;
    $grade[] = $obj->grade;
    $species[] = $obj->species;
    $woodDesc[] = $obj->wooddesc;
    $prefix[] = $obj->prefix;
}

$pricing = array(
     $d1d2,
     $grade,
     $species,
     $woodDesc,
     $prefix
     );

The array works as I can use print_r or var_dump and see that I have all that I need with the array. But then I need to sort the pricing array by three of the inner arrays. First, the $d1d2, then the $grade, and then the $species.

I have tried using multisort and ksort and even msort, but I can't get anything to work. Can someone point me in the right direction?

This is the output of my print_f:

Array ( [0] => Array ( [0] => 2X10 [1] => 2X10 [2] => 2X10 [3] => 2X10 [4] => 2X12 [5] => 2X12 [6] => 2X12 [7] => 2X12 [8] => 2X4 [9] => 2X4 [10] => 2X4 [11] => 2X4 [12] => 2X4 [13] => 1X4 [14] => 1X6 [15] => 2X10 [16] => 2X10 [17] => 2X10 [18] => 2X10 [19] => 2X12 [20] => 2X12 [21] => 2X12 [22] => 2X12 [23] => 2X4 [24] => 2X4 [25] => 2X4 [26] => 2X4 [27] => 2X4 [28] => 2X6 [29] => 2X6 [30] => 2X6 [31] => 2X6 [32] => 2X6 [33] => 2X 8 [34] => 2X8 [35] => 2X 8 [36] => 2X8 [37] => 2X 8 [38] => 4X8 [39] => 4X8 [40] => 4X8 [41] => 4X8 [42] => 4X8 [43] => 4X8 [44] => 4X8 [45] => 4X8 [46] => 4X8 [47] => 4X8 [48] => 4X8 [49] => 4X8 [50] => 4X8 [51] => 4X8 [52] => 4X8 [53] => 4X8 [54] => 4X8 [55] => 4X8 [56] => 4X8 [57] => 4X8 [58] => 4X8 [59] => 4X8 [60] => 4X8 [61] => 4X8 [62] => 4X8 [63] => 4X8 [64] => 4X8 [65] => 4X8 [66] => 4X8 [67] => 4X8 [d1d2] => ) [1] => Array ( [0] => #2 [1] => #2 [2] => #2 [3] => #2 [4] => #2 [5] => #2 [6] => #2 [7] => #2 [8] => #2 [9] => #2 [10] => #2 [11] => #2 [12] => #2 [13] => [14] => [15] => #2 [16] => #2 [17] => #2 [18] => #2 [19] => #2 [20] => #2 [21] => #2 [22] => #2 [23] => #2 [24] => #2 [25] => #2 [26] => #2 [27] => #2 [28] => #2 [29] => #2 [30] => #2 [31] => #2 [32] => #2 [33] => [34] => #2 [35] => [36] => #2 [37] => [38] => [39] => [40] => [41] => [42] => [43] => [44] => [45] => [46] => [47] => [48] => [49] => [50] => [51] => [52] => [53] => [54] => [55] => [56] => [57] => [58] => [59] => [60] => [61] => [62] => [63] => [64] => [65] => [66] => [67] => [d1d2] => ) [2] => Array ( [0] => HEM FIR [1] => HEM FIR [2] => HEM FIR [3] => HEM FIR [4] => HEM FIR [5] => HEM FIR [6] => HEM FIR [7] => HEM FIR [8] => SPF [9] => SPF [10] => SPF [11] => SPF [12] => SPF [13] => SPF [14] => SPF [15] => HEM FIR [16] => HEM FIR [17] => HEM FIR [18] => HEM FIR [19] => HEM FIR [20] => HEM FIR [21] => HEM FIR [22] => HEM FIR [23] => SPF [24] => SPF [25] => SPF [26] => SPF [27] => SPF [28] => SPF [29] => SPF [30] => SPF [31] => SPF [32] => SPF [33] => SPF [34] => SPF [35] => SPF [36] => SPF [37] => SPF [38] => SYP [39] => SYP [40] => SYP [41] => SYP [42] => SYP [43] => SYP [44] => SYP [45] => SYP [46] => FIR [47] => SYP [48] => FIR [49] => SYP [50] => FIR [51] => FIR [52] => FIR [53] => FIR [54] => FIR [55] => SYP [56] => SYP [57] => SYP [58] => SYP [59] => SYP [60] => SYP [61] => SYP [62] => SYP [63] => SYP [64] => SYP [65] => SYP [66] => SYP [67] => SYP [d1d2] => ) [3] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => UTILITY GRADE [14] => UTILITY GRADE [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => [38] => RSH 4-PLY [39] => RSH [40] => RSH [41] => BCX [42] => RSH 4-PLY [43] => RSH [44] => RSH [45] => S/F T&G [46] => RSH 4-PLY [47] => RSH 4-PLY [48] => RSH 4-PLY [49] => RSH [50] => RSH [51] => RSH [52] => ACX [53] => ACX [54] => ACX [55] => ACX [56] => ACX [57] => ACX [58] => BCX [59] => BCX [60] => BCX [61] => RSH 4-PLY [62] => RSH [63] => RSH [64] => RSH 4-PLY [65] => RSH [66] => RSH [67] => S/F T&G [d1d2] => ) [4] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => [38] => [39] => [40] => [41] => [42] => [43] => [44] => [45] => [46] => FSC MIXED CR [47] => [48] => [49] => [50] => [51] => [52] => [53] => [54] => [55] => [56] => [57] => [58] => [59] => [60] => [61] => FSC MIXED CR [62] => FSC MIXED CR [63] => FSC MIXED CR [64] => [65] => [66] => [67] => [d1d2] => ) )

When I try to sort it, I get no change. I deleted the multisort method that I was using and tried something else which didn't work either. So, I don't have it any longer to include the code for it as well..

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongtanhe4607 2014-12-03 21:37
    最佳回答

    Are you sure the array_multisort doesn't work?

    array_multisort(
            $pricing->d1d2, SORT_ASC, SORT_NUMERIC,
            $pricing->grade, SORT_ASC, SORT_NUMERIC,
            $pricing->species, SORT_ASC, SORT_NUMERIC
    );
    

    or

    array_multisort(
            $pricing[0], SORT_ASC, SORT_NUMERIC,
            $pricing[1], SORT_ASC, SORT_NUMERIC,
            $pricing[2], SORT_ASC, SORT_NUMERIC
    );
    

    EDIT

    So much time without coding PHP and I miss that array_multisort must included all the members:

    array_multisort(
            $pricing[0], SORT_ASC, SORT_NUMERIC,
            $pricing[1], SORT_ASC, SORT_NUMERIC,
            $pricing[2], SORT_ASC, SORT_NUMERIC,
            $pricing[3], SORT_ASC, SORT_NUMERIC,
            $pricing[4], SORT_ASC, SORT_NUMERIC
    );
    

    UPDATE

    These are the sorting type supported by array_multisort:

    SORT_REGULAR - Default. Compare elements normally (Standard ASCII)

    SORT_NUMERIC - Compare elements as numeric values

    SORT_STRING - Compare elements as string values

    SORT_LOCALE_STRING - Compare elements as string, based on the current locale (can be changed using setlocale())

    SORT_NATURAL - Compare elements as strings using "natural ordering" like natsort()

    SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题