简体   繁体   English

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

[英]How to sort a multidimensional array on several different keys in 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. 然后,我需要对在3个不同键上创建的数组进行排序。 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. 该数组可以正常工作,因为我可以使用print_rvar_dump并看到我拥有该数组所需的一切。 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 . 首先是$d1d2 ,然后是$grade ,然后是$species

I have tried using multisort and ksort and even msort , but I can't get anything to work. 我已经尝试使用multisortksort甚至msort ,但我不能得到任何工作。 Can someone point me in the right direction? 有人可以指出我正确的方向吗?

This is the output of my print_f: 这是我的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. 我删除了我正在使用的multisort方法,并尝试了其他一些也不起作用的方法。 So, I don't have it any longer to include the code for it as well.. 所以,我不再需要包含它的代码了。

Are you sure the array_multisort doesn't work? 您确定array_multisort不起作用吗?

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: 这么多时间没有编写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
);

UPDATE UPDATE

These are the sorting type supported by array_multisort : 这些是array_multisort支持的排序类型:

SORT_REGULAR - Default. SORT_REGULAR-默认。 Compare elements normally (Standard ASCII) 正常比较元素(标准ASCII)

SORT_NUMERIC - Compare elements as numeric values SORT_NUMERIC-将元素比较为数值

SORT_STRING - Compare elements as string values SORT_STRING-比较元素为字符串值

SORT_LOCALE_STRING - Compare elements as string, based on the current locale (can be changed using setlocale()) SORT_LOCALE_STRING-根据当前语言环境将元素比较为字符串(可以使用setlocale()进行更改)

SORT_NATURAL - Compare elements as strings using "natural ordering" like natsort() SORT_NATURAL-使用像natsort()这样的“自然顺序”将元素比较为字符串

SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively SORT_FLAG_CASE-可以与SORT_STRING或SORT_NATURAL组合(按位或)以区分大小写地对字符串进行排序

I've handled cases like that by writing a sort method and using usort(); 我已经通过编写排序方法并使用usort()处理了类似的情况; it's easier for me to think of sorting as code 对我而言,将排序视为代码更容易

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

That puts data into sorted order; 数据按排序顺序排列; now when you build up the separate arrays they will be built in sorted order. 现在,当您构建单独的阵列时,它们将按排序顺序构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM