2014-09-09 4 views
-1

Drupal 데이터베이스의 연관 배열을 json으로 인코딩 할 수있는 다차원 배열로 변환하려고합니다.배열에서 다차원 배열 만들기

$notifications =` 

    Array 
    (
    [0] => Array 
     (
      [rfp_id] => RFP-013-2014(C) 
      [notification_type] => due_date 
     ) 

    [1] => Array 
     (
      [rfp_id] => RFP-013-2014(C) 
      [notification_type] => changes 
     ) 

    [2] => Array 
     (
      [rfp_id] => RFP-013-2014(C) 
      [notification_type] => due_date 
     ) 

    [3] => Array 
     (
      [rfp_id] => RFP-014-2014(C) 
      [notification_type] => due_date 
     ) 

    [4] => Array 
     (
      [rfp_id] => RFP-014-2014(C) 
      [notification_type] => changes 
     ) 
    ) 

나는 rfp_id 필드에 의해 그룹을 좋아하고 같은 끝낼 것 :

Array (
    [0]=> Array (
    ["rfp_id"]=>"RFP-014-2014" 
    ["notification_type"]=> 
    Array (
     [0]=> "date_due" 
     [1]=> "changes" 
    ) 
) 
) 

가 어떻게이 배열을 반복이를 만드는 것

내가 시작?

+1

기본적으로 http://stackoverflow.com/questions/25557428/custom-formatted-json-from-mysql-pdo-for-use-in-nvd3-js/25557722 –

+0

과 같은 내용입니다. 어떤 노력을 했습니까? 이 작업을 수행? –

+0

@MikeBrant 배열을 반복 할 때 몇 가지 옵션을 시도했습니다. 내가 가진 가장 가까운 것은 각 notification_type을 배열이 아닌 하나의 배열로 배열 한 것입니다. – sho

답변

1

배열의 차원에 변화가 없습니다. 단지 재구성 된 것입니다. 다음과 같이 할 수 있습니다 :

foreach ($notifications as $notification) 
{ 
    $rfp_id = $notification['rfp_id']; 
    $newArray[$rfp_id]['rfp_id'] = $rfp_id; 
    $newArray[$rfp_id]['notification_type'][] = $notification['notification_type']; 
} 
echo '<pre>'.print_r($newArray,TRUE).'</pre>'; 

내가 알기로 지정한대로 약간 다르게 처리했습니다. 당신은 숫자 키를 원하는 경우에 당신은 할 수 :

$newArray = array_values($newArray); 
+0

몇 가지주의 오류 만 있습니다. –

+0

@JonathanKuhn : 아니요. 전 똑같이 게시 할 예정이었습니다. 상위 레벨의 존재 여부 또는 배열 exix가 통지를 생성하지 않더라도 배열 인덱스를 즉석에서 작성합니다. – AbraCadaver

+0

일단 print_f를 print_r로 변경했는데 이것은 완벽하게 연결되었습니다. 일단 array_values를 호출하면 정확히 내가 찾고있는 출력을 얻었습니다! – sho

0

다음과 같이 작동합니다.

$ array = array();

//loop over the array 
foreach($notifications as $row){ 
    //if this rfp id doesn't exist, add it 
    if(!isset($array[$row['rfp_id']])){ 
     //add the first entry in the array 
     $array[$row['rfp_id']] = array(
       'rfp_id'=>$row['rfp_id'], 
       'notification_type'=>array(
        $row['notification_type'] 
       ) 
      ); 
    } else { 
     //rfp_id exists, so just append the notification type 
     $array[$row['rfp_id']]['notification_type'][] = $row['notification_type']; 
    } 
} 
//get just the values to reset the first level keys to numeric. 
$array = array_values($array); 

//display the array 
echo '<pre>'.print_r($array,true).'</pre>'; 
+0

'foreach ($ array as $ entry) {$ entry [ "notification_type"] = array_unique ($ entry [ "notification_type"]); }''array_values' 다음에 중복 된 notification_type을 제거합니다. –

1
<? 
$result = array(); 

foreach ($notifications as $key => $note) { 
    $result[$note['rfp_id']]['rfp_id'] = $note['rfp_id']; 
    $result[$note['rfp_id']]['notification_type'][] = $note['notification_type']; 

} 

echo '<pre>'; 
print_r($result); 
echo '</pre>'; 

?> 

그것을해야한다. 열쇠를 숫자로 다시 설정할 필요가 없다고 가정하면 데이터베이스에 삽입하기 때문에.