在PHP中对多维数组中的子数组进行排序


Sorting a sub-array in a multidimensional array in PHP

我知道有人问过类似的问题,但即使看了所有的问题,我似乎也无法做到这一点。我认为它只是比我发现的其他例子稍微复杂一点。我知道有人会说这是一个重复的问题,但我已经非常努力地从我迄今为止看到的例子中得到了答案,所以提前道歉!

因此,在PHP中给定这个多维数组$results_display(下面的var_dump),子数组"#results"有5个成员,我想根据"#changed"字符串中的值对这5个成员进行排序(降序)。

有人能帮帮一个头撞桌子几天的女孩吗?

非常感谢!!!

我尝试的是在var_dump下面。我用标题注释了这一部分,试图让第一部分发挥作用。

$results_display =
array(8) {
   ["#theme"]=> string(18) "hs_filters_results"
   ["#title"]=> string(18) "On-Demand Webinars"
   ["#body"]=> NULL
   ["#results"]=> array(5) {
     [0]=> array(3) {      
     ["#changed"]=> string(10) "1403279484"
     ["#theme"]=> string(17) "hs_filters_result"
     ["#result"]=> array(25) {
        ["#nid"]=> string(4) "2057"
        ["#node_type"]=> array(2) {
           ["machine_name"]=> string(7) "webinar"
           ["name"]=> string(7) "Webinar" }
        ["#title"]=> string(61) "7 Critical Reasons to Automate Handling of IBM i Spool Files "
        ["#brand_nid"]=> string(2) "29"
        ["#brand_machine_name"]=> string(5) "brand"
        ...  }
   }
...

}

// Obtain a list of columns for the results array
foreach ($results_display as $key => $row) {
  $changed[$key]  = $row['changed'];
  //$title[$key] = $row['title'];
} 
// Sort the data with date changed descending, title ascending
// Add $results_display as the last parameter, to sort by the common key
//array_multisort($changed, SORT_DESC, $title, SORT_ASC, $results_display);
array_multisort($changed, SORT_DESC, $results_display);
usort($results_display['results'], function ($a, $b) {
    return $a['changed'] - $b['changed'];
});

这应该会让你开始。有关更多可能的排序选项,请参阅https://stackoverflow.com/a/17364128/476.