简体   繁体   English

PHP - 排序多维数组

[英]PHP - Sorting multi dimensional array

I have a multi dimensional array 我有一个多维数组

Array
 (
[M] => Array
    (
        [0] => Array
            (
                [first] => Paul
                [last] => Martha
                [progress] => 100
                [id] => 85162
                [category] => M
            )

        [1] => Array
            (
                [first] => Bob
                [last] => Marley
                [progress] => 47
                [id] => 846523
                [category] => M
            )

    )

[M~F1] => Array
    (
        [0] => Array
            (
                [first] => Kenneth
                [last] => Rodriguez
                [progress] => 42
                [id] => 110273
                [category] => M~F1
            )

        [1] => Array
            (
                [first] => Jenifer
                [last] => Lopez
                [progress] => 0
                [id] => 485623
                [category] => M~F1
            )

    )

)

However, I want each of the smaller arrays to sort themselves alphabetically based on keys from he array inside of them. 但是,我希望每个较小的数组根据其内部数组的键按字母顺序排序。 Take array M for example. 以数组M为例。 I want PHP to organize its subcompenets by alphabetizing the Last name componet. 我希望PHP通过按字母顺序排列姓氏组件来组织其子组件。 So, array M would organize its subarrays by alphabetizing the subarray of the subbaray. 因此,数组M将通过按字母顺序排列子数组的子数组来组织其子数组。 Once complete, it should look like this: 完成后,它应如下所示:

Array
(
[M] => Array
    (
    [0] => Array
            (
                [first] => Bob
                [last] => Marley
                [progress] => 47
                [id] => 846523
                [category] => M
            )

        [1] => Array
            (
                [first] => Paul
                [last] => Martha
                [progress] => 100
                [id] => 85162
                [category] => M
            )

    )

[M~F1] => Array
    (
        [0] => Array
            (
        [first] => Jenifer
                [last] => Lopez
                [progress] => 0
                [id] => 485623
                [category] => M~F1
            )

        [1] => Array
            (
                [first] => Keneth
                [last] => Rodriguez
                [progress] => 42
                [id] => 110273
                [category] => M~F1
            )

    )

)

So, to review, I ned to organize the subsub array by looking at the subsubsub array, gathering its [last] key and sorting the subsub array from the alphabetization of the[last] key of the subsubsub array. 因此,回顾一下,我需要通过查看subsubsub数组来组织子数组,收集它的[last]键并从subsubsub数组的[last]键的alphabetization中对子子数组进行排序。

Let the array you've defined be $x. 让你定义的数组为$ x。

// first, define our comparison function
$cmp = function ($a, $b) {
  // sort by last name, first name
  $a = trim($a['last'] . ' ' . $a['first']);
  $b = trim($b['last'] . ' ' . $b['first']);
  return strcmp($a, $b);
};
foreach ($x as &$subx) {
  // initialize sub array with reference
  usort($subx, $cmp);
}

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

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