2013-10-02 39 views
0

在我的項目中,我有以下幾種數組。請幫助我找到解決方案。 數組在下面給出,請參考。 看起來像跟隨。將數組轉換爲另一個數組

[0] => Array 
    (
      [Notification] => Array 
       (
        [id] => 135 
        [notification_date] => 2013-09-18 
        [short_desc] => dsfdfdfdf 
        [long_desc] => 
       ) 

      [NotificationProduct] => Array 
       (
        [0] => Array 
         (
          [id] => 41 
          [notification_id] => 135 
          [product_id] => 20 
          [Product] => Array 
           (
            [id] => 20 
            [category_id] => 2 
            [name] => asasasa 
           ) 

        ) 

        [1] => Array 
         (
         [id] => 42 
         [notification_id] => 135 
         [product_id] => 21 
         [Product] => Array 
           (
            [id] => 21 
            [category_id] => 2 
            [name] => corn flakcf 

           ) 

         ) 

       ) 

    ) 

我有以上類型array.Now我想把它轉換到以下方式:

[0] => Array 
    (
      [Notification] => Array 
       (
        [id] => 135 
        [notification_date] => 2013-09-18 
        [short_desc] => dsfdfdfdf 
        [long_desc] => 
       ) 

      [NotificationProduct] => Array 
       (
        [0] => Array 
         (
          [id] => 41 
          [notification_id] => 135 
          [product_id] => 20 
          [name] => asasasa 
        ) 

        [1] => Array 
         (
         [id] => 42 
         [notification_id] => 135 
         [product_id] => 21 
         [name] => corn flakcf 
         ) 

       ) 
) 
Is there any way to convert it into that kind of array. 

謝謝!

+0

你應該表現出你的方法 –

+0

有是一種方式:通過修改它來匹配y,通過原始數組循環我們的預期結構 –

回答

0

嘗試用以下:

  $counter = 0; 
      $responseArray = array(); 
      foreach($notifications as $notification){ 
       $responseArray[$counter]['Notification'] = $notification['Notification']; 
       $counter1 = 0; 
       foreach($notification['NotificationProduct'] as $notificationProduct){ 
        $responseArray[$counter]['NotificationProduct'][$counter1]['product_id'] = $notificationProduct['product_id']; 
        $responseArray[$counter]['NotificationProduct'][$counter1]['product_name'] = $notificationProduct['Product']['name']; 
        $responseArray[$counter]['NotificationProduct'][$counter1]['product_desc'] = $notificationProduct['Product']['description']; 
        $counter1++; 
       } 
       $counter++; 
      } 
0
$name = $array[0]['NotificationProduct'][0]['product']['name'] 
    unset($array[0]['NotificationProduct'][0]['product']) 
    $array[0]['NotificationProduct'][0]['name'] = $name; 

執行相同的$array[0]['NotificationProduct'][1]['product']

+0

不,它不能幫助我 –

+0

你的根數組有數字索引嗎?如果是這樣,然後嘗試使用'$ array [0]'而不是'$ array'。我已經測試了代碼,它正在爲我工​​作。 – Sage

+0

是否有可能與foreach循環? –

0

假設你的數組名$notices,試試這個:

foreach($notices AS $key => $val) 
{ 
    if($key=='NotificationProduct') 
    { 
     foreach($val AS $idx => $np) 
     { 
      $notices[$key][$idx]['name'] = $notices[$key][$idx]['Product']['name']; 
      unset($notices[$key][$idx]['Product']); 
     } 
    } 
} 
相關問題