我有一个这样的数组:
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个字符的匹配值,而值是前一个数组中匹配值的计数,一直在讨价还价。 非常感谢任何帮助!
答案 0 :(得分:2)
这有效(虽然新数组不保留时间,这可能更合适):</ p>
$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]];
}
}