1

I have this array:

    $test['name1'][256]=[
       'lead_data'=>[
           'date'=>'13.03.2019',
           'address'=>'addr1'
       ]
   ];
   $test['name1'][257]=[
       'lead_data'=>[
           'date'=>'12.03.2019',
           'address'=>'addr2'
       ]
   ];
   $test['name2'][259]=[
       'lead_data'=>[
           'date'=>'15.03.2019',
           'address'=>'addr4'
       ]
   ];
   $test['name2'][260]=[
       'lead_data'=>[
           'date'=>'18.03.2019',
           'address'=>'addr5'
       ]
   ];

There are two groups 'name1' and 'name2'. Need sort inside arrays by 'date' , i did it:

function array_sort_inner_array($array, $on, $order=SORT_ASC){
          $new_array = array();
          $sortable_array = array();
          if (count($array) > 0) {
              foreach ($array as $k => $v) {
                  if (is_array($v['lead_data'])) {
                      foreach ($v['lead_data'] as $k2 => $v2) {
                          if ($k2 == $on) {
                              $sortable_array[$k] = $v2;
                          }
                      }
                  } else {
                      $sortable_array[$k] = $v['lead_data'];
                  }
              }
              switch ($order) {
                  case SORT_ASC:
                      asort($sortable_array);
                      break;
                  case SORT_DESC:
                      arsort($sortable_array);
                      break;
              }
              foreach ($sortable_array as $k => $v) {
                  $new_array[$k] = $array[$k];
              }
          }
          return $new_array;
      }



 $testSort =[];
   foreach ($test as $k=>$t){
       $testSort[$k]=array_sort_inner_array($t,'date',SORT_DESC);
   }

And not i want sort groups 'name1' and 'name2' by 'date' column. So 'name2' must be first and 'name1' group must be second because 'name2' has date '18.03.2019' and it is the highest value. Not have idea how to do it. Please help me, thanks!

  • 3
    Possible duplicate of https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php – 04FS Mar 13 '19 at 10:49
  • Possible duplicate of [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – Dave Mar 13 '19 at 11:05
  • After sorting the inner array you can do `usort` and check the max after do array_column on 'lead_data' and 'date' so the "name2" will be before "name1" – dWinder Mar 13 '19 at 11:07
  • I get dates from column 'date': `$dates=[]; foreach ($testSort as $key=>$item){ foreach ($item as $i){ $dates[]=$i['lead_data']['date']; } }` But cant understand about usort – Anastasia Colobets Mar 13 '19 at 11:12

1 Answers1

0

I meant use array_column and uasort as:

function getMaxDate($e) {
    return max(array_column(array_column($e, "lead_data"), "date"));
}

uasort($test, function ($a, $b) {return strcmp(getMaxDate($b), getMaxDate($a));;});

Notice - you still need to sort the inner array if you wish - I only showed how to do outer sort for max date - inner sort can be done as:

foreach($test as &$e)
    uasort($e, function ($a, $b) {return strcmp($a['lead_data']['date'], $b['lead_data']['date']);;});

Reference: uasort, array-column

dWinder
  • 11,597
  • 3
  • 24
  • 39