按日期对 php 中的 3 个多维数组进行排序

Sorting 3 multi-dimention array in php by dates

不确定之前是否有人问过这个问题,但谁能帮我按日期对数组进行排序 (latest_user_activity_date)。我试图在互联网上搜索,但未能对此进行排序。我试过使用 array_multisort 函数,但它只对第一个索引 (admin) 进行排序。我正在使用 PHP 5.3 版本。

Array
  (
  [admin] => Array
    (
    [0] => Array
      (
      [id] => 3
      [user_type] => Accountant
      [latest_user_activity_date] => 2000 - 00 - 00 00: 00: 00
      [full_link] => admin / billing_contacts.app
      )
    [1] => Array
      (
      [id] => 1
      [user_type] => Accountant
      [latest_user_activity_date] => 2018 - 04 - 19 13: 18: 59.112704
      [full_link] => admin / billing_contacts.app
      )
    [2] => Array
      (
      [id] => 2
      [user_type] => Accountant
      [latest_user_activity_date] => 2018 - 04 - 15 13: 18: 59.112704
      [full_link] => admin / client_upload.app
      )
    )
  [invoice] => Array
    (
    [0] => Array
      (
      [id] => 3
      [user_type] => Accountant
      [latest_user_activity_date] => 2000 - 00 - 00 00: 00: 00
      [full_link] => admin / create_invoice.app
      )
    [1] => Array
      (
      [id] => 1
      [user_type] => Accountant
      [latest_user_activity_date] => 2018 - 04 - 19 13: 18: 59.112704
      [full_link] => admin / list_invoice.app
      )
    [2] => Array
      (
      [id] => 2
      [user_type] => Accountant
      [latest_user_activity_date] => 2018 - 04 - 20 13: 18: 59.112704
      [full_link] => admin / delete_invoice.app
      )
    )

  )

期望的输出:

Array ( [invoice] => Array ( [8] => Array ( [id] => 2 [user_type] => Accountant [latest_user_activity_date] => 2018-04-20 13:18:59.112704 [full_link] => admin/delete_invoice.app ) [7] => Array ( [id] => 1 [user_type] => Accountant [latest_user_activity_date] => 2018-04-19 13:18:59.112704 [full_link] => admin/list_invoice.app ) [6] => Array ( [id] => 3 [user_type] => Accountant [latest_user_activity_date] => 2000-00-00 00:00:00 [full_link] => admin/create_invoice.app ) ) [admin] => Array ( [4] => Array ( [id] => 1 [user_type] => Accountant [latest_user_activity_date] => 2018-04-19 13:18:59.112704 [full_link] => admin/billing_contacts.app ) [5] => Array ( [id] => 2 [user_type] => Accountant [latest_user_activity_date] => 2018-04-15 13:18:59.112704 [full_link] => admin/client_upload.app ) [2] => Array ( [id] => 3 [user_type] => Accountant [latest_user_activity_date] => 2000-00-00 00:00:00 [full_link] => admin/billing_contacts.app ) ) )

如您所见,'invoice' 索引现在位于顶部,因为它在 'latest_user_activity_date' 字段上具有最新值。如果有人知道如何解决这个问题,请提供帮助。谢谢!

这是解决上述问题的方法。希望对其他人有帮助。

感谢回复的人。

function customSortMultiArray($orig_array) {
   $master_arr = array();
   $i = 0;
   foreach ($orig_array as $key1 => $arr1) {
    $temp_arr = array();
    foreach ($arr1 as $key2 => $arr2) {
        $date = date('Y-m-d H:i:s', strtotime($arr2['latest_user_activity_date']));
        //$temp_arr[$i][$date] = $arr2;
        //$temp_arr[$i][$date]['key'] = $key2;
        $temp_arr[$i]['date'] = $date;
        $temp_arr[$i]['key'] = $key2;
        $i++;
    }
    array_multisort($temp_arr, SORT_DESC);
    $arr3 = array();
    foreach ($temp_arr as $arr) {
        $arr3[$arr['key']] = $arr1[$arr['key']];
    }        
    $master_arr[$key1] = $arr3;
}  
$temp_arr2 = array();
$i = 0;
foreach ($master_arr as $key1 => $arr1) {
    foreach ($arr1 as $key2 => $arr2) {
        $date = date('Y-m-d H:i:s', strtotime($arr2['latest_user_activity_date']));
        break;
    }
    $temp_arr2[$i]['date'] = $date;
    $temp_arr2[$i]['key'] = $key1;
    $i++;
}
array_multisort($temp_arr2, SORT_DESC);
$final_arr = array();
foreach ($temp_arr2 as $arr) {
    $final_arr[$arr['key']] = $master_arr[$arr['key']];
} }

这个自定义排序函数可以满足您的需要:

function customSort(array $array)
{
    // Iterate through and sort second level array by most recent date
    foreach ($array as $key => &$value) {
        usort($value, function ($a, $b) {
            return strtotime($a['latest_user_activity_date']) < strtotime($b['latest_user_activity_date']) ? 1 : -1;
        });
    }

    // Sort first level array by date descending of first (most recent) entry
    uasort($array, function ($a, $b) {
        return strtotime($a[0]['latest_user_activity_date']) < strtotime($b[0]['latest_user_activity_date']) ? 1 : -1;
    });

    return $array;
}

这是一个工作示例:https://3v4l.org/oZvJv