-1

我有一个这样的数组:

Array
(
    [0] => Saturday 19th January 2013 07:59:57 PM
    [1] => Sunday 20th January 2013 07:00:26 PM
    [2] => Monday 21st January 2013 02:08:15 PM
    [3] => Monday 21st January 2013 02:11:00 PM
    [4] => Monday 21st January 2013 02:41:02 PM
    [5] => Monday 21st January 2013 02:42:14 PM
    [6] => Monday 21st January 2013 03:03:34 PM
    [7] => Monday 21st January 2013 03:07:39 PM
    [8] => Monday 21st January 2013 03:37:11 PM
    [9] => Monday 21st January 2013 03:37:52 PM
    [10] => Monday 21st January 2013 04:14:08 PM
    [11] => Monday 21st January 2013 04:16:38 PM
    [12] => Monday 21st January 2013 04:19:18 PM
    [13] => Monday 21st January 2013 05:45:17 PM
    [14] => Monday 21st January 2013 06:39:04 PM
    [15] => Monday 21st January 2013 08:01:56 PM
    [16] => Monday 21st January 2013 08:03:23 PM
    [17] => Monday 21st January 2013 08:12:15 PM
    [18] => Monday 21st January 2013 08:15:20 PM
    [19] => Monday 21st January 2013 08:29:57 PM
    [20] => Monday 21st January 2013 09:57:25 PM
    [21] => Tuesday 22nd January 2013 01:57:59 AM
    [22] => Tuesday 22nd January 2013 06:39:34 AM
    [23] => Tuesday 22nd January 2013 11:38:25 AM
    [24] => Tuesday 22nd January 2013 03:21:08 PM
    [25] => Tuesday 22nd January 2013 04:09:36 PM
}

我想对这个数组进行分组,以便得到:

Array
(
    [Saturday 19th January 2013 07:59:57 PM] => 1
    [Sunday 20th January 2013 07:00:26 PM] => 1
    [Monday 21st January 2013 02:08:15 PM] => 19
    [Tuesday 22nd January 2013 01:57:59 AM] => 5
   }

这里的键应该是前一个数组中匹配 20-23 个字符的匹配值,值是前一个数组中匹配值的计数,我一直在努力解决这个问题。将不胜感激任何帮助!

4

1 回答 1

2

这有效(尽管新数组不能保持时间,这可能更可取):

$array = array('Saturday 19th January 2013 07:59:57 PM', 'Sunday 20th January 2013 07:00:26 PM', 'Monday 21st January 2013 02:08:15 PM', 'Monday 21st January 2013 02:11:00 PM', 'Monday 21st January 2013 02:41:02 PM', 'Monday 21st January 2013 02:42:14 PM', 'Monday 21st January 2013 03:03:34 PM', 'Monday 21st January 2013 03:07:39 PM', 'Monday 21st January 2013 03:37:11 PM', 'Monday 21st January 2013 03:37:52 PM', 'Monday 21st January 2013 04:14:08 PM', 'Monday 21st January 2013 04:16:38 PM', 'Monday 21st January 2013 04:19:18 PM', 'Monday 21st January 2013 05:45:17 PM', 'Monday 21st January 2013 06:39:04 PM', 'Monday 21st January 2013 08:01:56 PM', 'Monday 21st January 2013 08:03:23 PM', 'Monday 21st January 2013 08:12:15 PM', 'Monday 21st January 2013 08:15:20 PM', 'Monday 21st January 2013 08:29:57 PM', 'Monday 21st January 2013 09:57:25 PM', 'Tuesday 22nd January 2013 01:57:59 AM', 'Tuesday 22nd January 2013 06:39:34 AM', 'Tuesday 22nd January 2013 11:38:25 AM', 'Tuesday 22nd January 2013 03:21:08 PM', 'Tuesday 22nd January 2013 04:09:36 PM');

foreach ($array as $date)
{

  $name = date('l jS F Y', strtotime($date));

  if (!isset($newArray[$name]))
  {
    $newArray[$name] = 1;
  }

  else
  {
    ++$newArray[$name];
  }

}

print_r($newArray);

如果您想要时间,请使用以下命令:

foreach ($array as $date)
{

  $name = date('l jS F Y', strtotime($date));

  $time = substr($date, -11);

  if (!isset($times[$name]))
  {
    $newArray[$name . ' ' . $times[$name] = $time] = 1;
  }

  else
  {
    ++$newArray[$name . ' ' . $times[$name]];
  }

}
于 2013-03-29T21:12:48.063 回答