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

时间:2014-12-03 21:30:33

标签: php arrays multidimensional-array

我正在从数据库返回的数组中创建一个多维数组。然后我需要在3个不同的键上对我创建的数组进行排序。所以,这是我的阵列。

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

数组工作正常,我可以使用print_rvar_dump,看看我拥有数组所需的全部内容。但后来我需要通过三个内部数组对定价数组进行排序。首先是$d1d2,然后是$grade,然后是$species

我尝试使用multisortksort甚至是msort,但我无法正常使用。有人能指出我正确的方向吗?

这是我的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] => ) )

当我尝试对它进行排序时,我没有变化。我删除了我正在使用的多重方法,并尝试了其他一些无法正常工作的方法。所以,我不再需要它来包含它的代码..

2 个答案:

答案 0 :(得分:2)

您确定array_multisort无效吗?

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

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

修改

没有编写PHP的时间太长了,我想念array_multisort必须包括所有成员:

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

更新

这些是array_multisort支持的排序类型:

SORT_REGULAR - 默认。通常比较元素(标准ASCII)

SORT_NUMERIC - 将元素比较为数值

SORT_STRING - 将元素比较为字符串值

SORT_LOCALE_STRING - 根据当前区域设置将元素比较为字符串(可以使用setlocale()更改)

SORT_NATURAL - 使用"自然排序"将元素作为字符串进行比较像natsort()

SORT_FLAG_CASE - 可以与SORT_STRING或SORT_NATURAL组合(按位或)以对字符串不区分大小写进行排序

答案 1 :(得分:2)

我通过编写排序方法并使用usort()来处理这种情况;我觉得排序为代码更容易

// sort data along 3 axes, each in increasing order
function comparData( $a, $b ) {
    if ($a->d1d2 < $b->d1d2) return -1;
    if ($a->d1d2 > $b->d1d2) return 1;
    if ($a->grade < $b->grade) return -1;
    if ($a->grade > $b->grade) return 1;
    if ($a->species < $b->species) return -1;
    if ($b->species > $b->species) return 1;
    return 0;
}

$data = $priceList->data;
usort($data, 'comparData');

将数据按排序顺序排列;现在当你构建单独的数组时,它们将按排序顺序构建。