2013-10-02 7 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']); 
     } 
    } 
} 
Смежные вопросы