2015-10-04 73 views
-1

我有一个需要排序的多维数组。该阵列是这样的:等价于MySQL的PHP​​ usort

$array[0] = $key1[0]['date']$key2['title'] 
$array[1] = $key1[0]['date']$key2['title'] 
$array[2] = $key2['title'] 

每个阵列项可具有多个日期($ KEY1 [0] [ '日期'],$ KEY1 [1] [ '日期'],等等)或者根本没有。当出现时,日期如下:2015-10-04T00:00。

我保存的strtotime($ KEY1 [0] [ '日期']),并在MySQL表中的每个数组元素的KEY2 [ '标题'],然后订购它像这样:

(CASE WHEN date < UNIX_TIMESTAMP(CURDATE()) THEN 0 ELSE 1 END) DESC, date ASC 

我需要直接在数组中复制结果,可能使用usort(); $ key1 [0] ['date']是排序的关键,这可能不存在。

规则应该是,如果日期在将来,它应该命令它升序(最接近的是第一个),如果日期在过去,它应该命令它降序。如果没有日期,它应该把这些条目放在中间(未来和现在之间)。

例如,如果今天是2015年10月4日,它应该排序:

  1. 2015年10月5日
  2. 2015年10月10日
  3. 2015年12月1日
  4. 无日期设置
  5. 没有日期为
  6. 2015年10月3日
  7. 2015年9月1日

谢谢。

+1

这引发异常'$数组[0] = $ key1的[0] [ '日期'] $ KEY2 [” title']' –

+0

所以数组的正确代码应该是$ key1 [0] ['date'] [$ key2] ['title']。但让我们得到正确的需求。 你想启用排序key2吗? – user2420647

+0

谢谢你指出。我需要对key1 [0]启用排序(按日期升序排序以便将来日期,降序排列过去的日期以及无日期的条目应该介于两者之间)。 – Marius

回答

0

我有一个解决方案。它可能不是最好的解决方案,但它确实按预期工作。

<?php 

# The array of dates 
$dates = array('2015-10-05', '2015-10-10', '2015-12-01', '2015-10-03', '2015-09-01', 'invalid date', ''); 

# I shuffle the array as it will be in disorder - you want to remove this line 
shuffle($dates); 

# I save the todays timestamp into a variable 
echo '<pre>'; print_r($dates) ; echo '</pre>'; 
$now = time(); 
$today = strtotime(date('Y-m-d'), $now); 
echo 'Today is: ' . date('Y-m-d', $now) . '<br>'; 

# Define the buffers 
$before = array(); 
$undef = array(); 
$after = array(); 

# Loop through all dates 
foreach ($dates as $i => $date): 
    # Get the timestamp for the date X 
    $time = strtotime($date); 

    # Check if timestamp lies before or after today or is undefined 
    if ($time === false || $time === -1): 
    $undef[] = $date; 
    elseif ($time <= $today): 
    $before[] = $date; 
    else: 
    $after[] = $date; 
    endif; 
endforeach; 

# Sort both arrays by value (descending per default) 
sort($before); 
sort($after); 

# Here we make the dates that lie before today ascending 
$before = array_reverse($before); 

# Some output - you can ommit these lines 
$sorted = array('after' => $after, 'undefined' => $undef, 'before' => $before); 
echo '<pre>'; print_r($sorted); echo '</pre>'; 

# This will merge all three arrays together - this is the final result 
$result = array_merge($after, $undef, $before); 
echo '<pre>'; print_r($result); echo '</pre>'; 

输出:

Array 
(
    [0] => invalid date 
    [1] => 
    [2] => 2015-10-03 
    [3] => 2015-10-10 
    [4] => 2015-12-01 
    [5] => 2015-09-01 
    [6] => 2015-10-05 
) 

Today is: 2015-10-04 

Array 
(
    [after] => Array 
     (
      [0] => 2015-10-05 
      [1] => 2015-10-10 
      [2] => 2015-12-01 
     ) 

    [undefined] => Array 
     (
      [0] => invalid date 
      [1] => 
     ) 

    [before] => Array 
     (
      [0] => 2015-10-03 
      [1] => 2015-09-01 
     ) 

) 

Array 
(
    [0] => 2015-10-05 
    [1] => 2015-10-10 
    [2] => 2015-12-01 
    [3] => invalid date 
    [4] => 
    [5] => 2015-10-03 
    [6] => 2015-09-01 
) 

你可以在这里进行测试:php tester tool