0

Not sure if this was asked before but can anyone help me sort below array by date (latest_user_activity_date). I was trying to search on the internet but I failed to sort this. I have tried using array_multisort function but it only sort the first index (admin). I am using PHP 5.3 version.

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
      )
    )

  )

Desired output:

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 ) ) )

as you can see the 'invoice' index is now on top because it has the latest value on 'latest_user_activity_date' field. Please if anyone knows how to solve this, kindly help. Thanks.!

Tony
  • 19
  • 4
  • Possible duplicate of [Sort Multi-dimensional Array by Value](https://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) – Matheus Cuba Apr 19 '18 at 11:26
  • Thanks @Matheus Cuba but I believe it is not duplicate to that. I'm using PHP 5.3 b d way. – Tony Apr 20 '18 at 02:29
  • Do you realise that this isn't actually a valid date - `2000-00-00 00:00:00`? – fubar Apr 20 '18 at 02:33
  • hi @fubar, it is just a dummy data. its still be going to be converted into a timestamp strtotime('2000-00-00 00:00:00'); – Tony Apr 20 '18 at 03:32
  • @Tony - it will still be converted, but not to anything you might expect.`strtotime('2000-00-00 00:00:00') ` is `1999-11-30` when converted back to a date. But fair enough. – fubar Apr 20 '18 at 04:04
  • Anyway, post the code you wrote to sort your array, and we might be able to give you some pointers. – fubar Apr 20 '18 at 04:05
  • @fubar yes thanks.. the initial value of it was empty but then I added 2000-00-00 00:00:00 for comparing purposes. I posted my answer below. This is my temporary solution. Just in case you have a better and faster one, please post it here. Thanks – Tony Apr 20 '18 at 05:02

2 Answers2

0

this is the solution to the above problem. Hope it will help others.

Thanks for those who replied.

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']];
} }
Tony
  • 19
  • 4
0

This custom sort function will do what you need:

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

And here's a working example: https://3v4l.org/oZvJv

fubar
  • 16,918
  • 4
  • 37
  • 43