2016-09-09 75 views
-4

示例代碼陣列:https://eval.in/639002PHP對於每個附加陣列

有客體(USER_ID = 1)購買多張(TICKET_ID = 1 & 2)在事件(event_id的= 11)。

我期待的結果格式:

[ticket] => Array 
(
    [1] => Ticket Name, *** 
    [2] => Ticket Name 
) 

代碼示例如下:

$data_db = array(
    array(
     'id' => 1, 
     'user_id' => 1, 
     'event_id' => 11, 
     'ticket_id' => 1, 
     'user_name' => 'guest 1' 
    ), 
    array(
     'id' => 2, 
     'user_id' => 2, 
     'event_id' => 11, 
     'ticket_id' => 1, 
     'user_name' => 'guest 2' 
    ), 
    array(
     'id' => 3, 
     'user_id' => 3, 
     'event_id' => 22, 
     'ticket_id' => 1, 
     'user_name' => 'guest 3' 
    ), 
    array(
     'id' => 4, 
     'user_id' => 1, 
     'event_id' => 11, 
     'ticket_id' => 2, 
     'user_name' => 'guest 1' 
    ) 
); 

$output = []; 

foreach ($data_db as $key => $row) { 

    $output[ $row['event_id'] ]['event'] = 'Event Name';  
    $output[ $row['event_id'] ]['attendee'][ $row['user_id'] ] = $row; 

    $output[ $row['event_id'] ]['attendee'][ $row['user_id'] ]['ticket'][ $row['ticket_id'] ] = 'Ticket Name'; 

} 

print_r($output); 

當前結果

Array 
(
    [11] => Array 
     (
      [event] => Event Name 
      [attendee] => Array 
       (
        [1] => Array 
         (
          [id] => 4 
          [user_id] => 1 
          [event_id] => 11 
          [ticket_id] => 2 
          [user_name] => guest 1 
          [ticket] => Array 
           (
            [2] => Ticket Name 
           ) 

         ) 

        [2] => Array 
         (
          [id] => 2 
          [user_id] => 2 
          [event_id] => 11 
          [ticket_id] => 1 
          [user_name] => guest 2 
          [ticket] => Array 
           (
            [1] => Ticket Name *** 
           ) 

         ) 

       ) 

     ) 

    [22] => Array 
     (
      [event] => Event Name 
      [attendee] => Array 
       (
        [3] => Array 
         (
          [id] => 3 
          [user_id] => 3 
          [event_id] => 22 
          [ticket_id] => 1 
          [user_name] => guest 3 
          [ticket] => Array 
           (
            [1] => Ticket Name 
           ) 

         ) 

       ) 

     ) 

) 
+0

請給例子,你想要什麼類型的輸出數組。 –

回答

0

,你首先可以通過挑選出票的數組開始其中機票屬於哪個用戶:

$output = []; 

foreach($data_db as $key=>$row) { 
    // Define new $tickets Array E.g ['guest_name']=>['ticket_id'] 
    $tickets[$row['user_name']][] = $row['ticket_id']; 
    $output [] = [ 
       'id'=>$row['id'], 
       'user_id'=>$row['user_id'], 
       'event_id'=>$row['event_id'], 
       'user_name'=>$row['user_name'], 
       'tickets'=>[] 
       ]; 
} 

你的$輸出門票會是什麼樣子:

Array 
(
    [guest 1] => Array 
     (
      [0] => 1 
      [1] => 2 
     ) 
    [guest 2] => Array 
     (
      [0] => 1 
     ) 
    [guest 3] => Array 
     (
      [0] => 1 
     ) 
) 

然後你可以將它們組合起來,就像這樣:

foreach($output as $k=>$v){ 
foreach($tickets as $guest=>$ticket){ 

    if($v['user_name'] == $guest){ 
     $output[$k]['tickets'] = $ticket; 
    } 

    } 
} 

你的輸出是:

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [user_id] => 1 
      [event_id] => 11 
      [user_name] => guest 1 
      [tickets] => Array 
       (
        [0] => 1 
        [1] => 2 
       ) 

     ) 

    [1] => Array 
     (
      [id] => 2 
      [user_id] => 2 
      [event_id] => 11 
      [user_name] => guest 2 
      [tickets] => Array 
       (
        [0] => 1 
       ) 
       ........