我有下面的数组
Array
(
[0] => Array
(
[reservation_time] => 08:30
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-14
)
[1] => Array
(
[reservation_time] => 09:00
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-14
)
[2] => Array
(
[reservation_time] => 09:30
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-14
)
[3] => Array
(
[reservation_time] => 08:30
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-15
)
[4] => Array
(
[reservation_time] => 09:00
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-15
)
[5] => Array
(
[reservation_time] => 09:30
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-15
)
[6] => Array
(
[reservation_time] => 08:30
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-16
)
)
数组应该是
Array(
[2011-07-14] => Array
(
[0] => Array
(
[reservation_time] => 08:30
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-14
)
[1] => Array
(
[reservation_time] => 09:00
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-14
)
)
[2011-07-15] => Array
(
[0] => Array
(
[reservation_time] => 08:30
[user_name] =>
[reason] =>
[comments] => recursive
[reservation_date] => 2011-07-15
)
)
如何循环此数组以获得所需的输出
的问候Nisanth
$new_array = array();
foreach($array as $item) {
$new_array[$item['reservation_date']][] = $item;
}
有时按子键(任何子键)分组多维数组是很好的。这基本上是可接受的答案,但重载了实际的数组:
print_r($array('reservation_date')); # group by subkey 'reservation_date'
代码(演示):$array = array(
0 => array(
'reservation_time' => '08:30',
'user_name' => '',
'reason' => '',
'comments' => 'recursive',
'reservation_date' => '2011-07-14',
),
1 => array(
'reservation_time' => '09:00',
'user_name' => '',
'reason' => '',
'comments' => 'recursive',
'reservation_date' => '2011-07-14',
),
2 => array(
'reservation_time' => '09:30',
'user_name' => '',
'reason' => '',
'comments' => 'recursive',
'reservation_date' => '2011-07-14',
),
3 => array(
'reservation_time' => '08:30',
'user_name' => '',
'reason' => '',
'comments' => 'recursive',
'reservation_date' => '2011-07-15',
),
4 => array(
'reservation_time' => '09:00',
'user_name' => '',
'reason' => '',
'comments' => 'recursive',
'reservation_date' => '2011-07-15',
),
5 => array(
'reservation_time' => '09:30',
'user_name' => '',
'reason' => '',
'comments' => 'recursive',
'reservation_date' => '2011-07-15',
),
6 => array(
'reservation_time' => '08:30',
'user_name' => '',
'reason' => '',
'comments' => 'recursive',
'reservation_date' => '2011-07-16',
),
);
// overload $array with a function
$array = function($key) use ($array) {
$r = array();
foreach($array as $v)
$r[$v[$key]][] = $v;
return $r;
};
// request subkey per parameter:
print_r($array('reservation_date'));
print_r($array('reservation_time'));
从我自己的PHP代码,我有这个函数。就叫make_assoc_from_list($your_array, 'reservation_date')
:
// makes associative array from normal list
// - $list - list
// - $key - key of each item which will be the associative key
//
function make_assoc_from_list($list, $key)
{
$assoc = array();
if ($list === null)
$list = array();
foreach ($list as $item)
$assoc[$item[$key]] = $item;
return $assoc;
}
$new = array();
foreach ($array as $entry) {
if (!array_key_exists($entry['reservation_date'], $new)) $new[$entry['reservation_date']] = array();
$new[$entry['reservation_date']][] = $entry;
}