2013-10-02 2 views
0

내 프로젝트에서 나는 array.Please 해결 방법을 찾으려면 나를 도와주세요. 어레이가 아래에 나와 있습니다. 참조하십시오. 다음과 같습니다.다른 배열로 배열 변환

[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] => 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

당신이 당신의 접근 방식 –

+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' 대신에'$ array [0]'을 시도하십시오. 나는 이미 코드를 테스트했고 그것은 나를 위해 일하고있다. – 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']); 
     } 
    } 
}