0

私のプロジェクトでは、次のような配列がありました。解決策を見つけるのを手伝ってください。以下に配列を示しますので、参考にしてください。FOLLOWINGのようです。

 [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

                                  )

                         )

                  )

    )

私は上記の種類の配列を持っています.今、私はそれを次の方法に変換したいと思います:

 [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.

ありがとう!

4

3 に答える 3

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++;
            }
于 2013-10-02T10:16:21.980 に答える
0

あなたの配列が name であると仮定して$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']);
        }
    }
}
于 2013-10-02T10:19:39.167 に答える
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']

于 2013-10-02T08:27:00.817 に答える