2011-07-22 63 views
0

我有以下陣列PHP數組操作

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

回答

7
$new_array = array(); 
foreach($array as $item) { 
    $new_array[$item['reservation_date']][] = $item; 
} 
+0

優雅,簡單,+1。 –

+0

我正要回答非常相似,+1給你 – redShadow

-1
$new = array(); 
foreach ($array as $entry) { 
    if (!array_key_exists($entry['reservation_date'], $new)) $new[$entry['reservation_date']] = array(); 
    $new[$entry['reservation_date']][] = $entry; 
} 
+0

我想你忘記了一點那裏... – tdammers

+0

是,按下TAB和空間.. 。和答案已提交:(所以請刪除downvote – rabudde

0

從我自己的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; 
} 
1

有時候真的很高興獲得由子分組多維數組 - 所有子項。這基本上就像接受的答案,但超載的實際陣列:

print_r($array('reservation_date')); # group by subkey 'reservation_date' 

碼(Demo):

$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'));