1

I have created method to sort array with values like this: array('regdate','birthday','editdate') which should sort the elements in the way that the elements containing word date should be moved to left like this array('regdate','editdate','birthday')

public function sortColumnsBySubstring($haystack, $substr){
   if ($haystack == $substr) {
          return 0;
      }
    return strpos($substr) !== false ? -1 : 1; 
 }

However it is not clear to me how to make this working. Example which I have found in php manual shows function with no arguments or closures - I use php version 5.2 so I cannot use closures.

All I can think up is this usort($date_cols, $this->sortColumnsBySubstring($value, 'date') but here $value is undefined so it's not solution.

Question is how to implement the function to work correctly?

John Boe
  • 3,016
  • 10
  • 32
  • 56
  • Perhaps, [this](http://stackoverflow.com/questions/8230538/pass-extra-parameters-to-usort-callback) could help you. – Muriano May 25 '16 at 11:03
  • Possible duplicate of [How to pass usort() a parameter?](http://stackoverflow.com/questions/29823045/how-to-pass-usort-a-parameter) – Muriano May 25 '16 at 11:05
  • https://3v4l.org/eVP1P check this – Ali May 25 '16 at 11:10
  • @ali this is interesting solution but the code does not work on my version of php because of closures. – John Boe May 25 '16 at 14:09
  • Are u using PHP before version 4. Because this will work in PHP 4,5,7. See http://php.net/manual/en/function.array-walk.php – Ali May 26 '16 at 05:12
  • @ali, sorry for delay. The problem was that I use php 5.2 and closures [link]http://php.net/manual/en/class.closure.php[/link] are from 5.3. Also I have found another solution so it is definitively solved now. Thanks. – John Boe May 26 '16 at 15:15

2 Answers2

2

You need to pass the callback as an array:

usort($date_cols, [$this, 'sortColumnsBySubstring']);

See Callbacks / Callables in PHP docs.

ShiraNai7
  • 5,751
  • 2
  • 20
  • 23
0

First solution is to my original question:

function cmp($a, $b)
{
    $adate = (strpos($a, 'date') !== false);
    $bdate = (strpos($b, 'date') !== false);
    if (!($adate ^ $bdate)) return strcmp($a, $b);
    return $adate ? -1 : 1;
}

$a = array('birthday', 'regdate', 'editdate');
usort($a, 'cmp');

Second solution uses splitting into two arrays, sort and then merge them back. I have tried to use more word related to time to identify the values related to time.

 private function getDateColumns(&$array)
 {
 $search_date_columns = array('date','datetime','timestamp','time','edited','changed','modified','created','datum');
 $result = array( array(), array() );
 foreach($array as $v1):
  $found = false;
  foreach($search_date_columns as $v2)
    if ( strpos($v1, $v2)!==false )
      { $found = true; break; }
  if ($found)
    $result[0][] = $v1;
  else
    $result[1][] = $v1;
 endforeach;
 return $result;
 }

Which is implemented like that:

$date_cols = array('regdate','time','editdate','createdate','personal','mojedatum','edited','test','modified','changed','pokus','timestamp','hlava');
$arrays = $this->getDateColumns($date_cols);
rsort($arrays[0]);
$date_cols = array_merge($arrays[0], $arrays[1]); 
unset($arrays);
print_r($date_cols);
John Boe
  • 3,016
  • 10
  • 32
  • 56