按日期

时间:2018-04-19 10:59:05

标签: html arrays array-multisort

不确定之前是否曾询问过,但是有人可以帮我按日期排序数组(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 ) ) )

正如您所见,“发票”索引现在位于顶部,因为它在“latest_user_activity_date”字段中具有最新值。如果有人知道如何解决这个问题,请帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

这是上述问题的解决方案。希望它能帮助别人。

感谢那些回复的人。

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']];
} }

答案 1 :(得分:0)

此自定义排序功能将满足您的需求:

doSomething()

这是一个有效的例子:https://3v4l.org/oZvJv