2014-09-03 2 views
0

나는 내가 JSON으로 뱉어있어 배열을하고는 다음과 같습니다 AngularJS와 : ID가 동일그룹 배열

{ 
id: 207 
order_id: 9325 
other_id: 3332 
} 
{ 
id: 207 
order_id: 4444 
other_id: 33233 
} 
{ 
id: 437 
order_id: 9325 
other_id: 22233 
} 

경우, 나는 그것을보고 싶어 이 같은 : 등

id: 207 
    { 
     order_id: 9325 
     other_id: 3332 
    }, 
    { 
     order_id: 4444 
     other_id: 33233 
    } 

..

json으로 뱉어

지금까지 내 백엔드 코드는 다음과 같습니다

foreach($others as $other) 
    { 

     if(!empty($other->table_one)) 
     { 
      continue; 
     } 
     else 
     { 
      $checks[] = array('id' => $other->id, 'order_id' => $other->order_id, 'other_id' => $other->other_id); 
     } 
    } 

답변

0

$other->id을 배열 키로 사용하고 order_idother_id을 포함하는 배열을 추가하려면 배열에 다른 레벨이 필요합니다.

foreach($others as $other) { 
    if(!empty($other->table_one)) { 
     continue; 

    // Create a blank array before trying to use it 
    if(!array_key_exists($other->id, $checks)) 
     $checks[$other->id] = array(); 

    // Add this entry to your array 
    $checks[$other->id][] = array(
     'order_id' => $other->order_id, 
     'other_id' => $other->other_id 
    ); 
} 

Example (PHP 출력), 그리고 만약 당신이 json_encode()이 : 대신이 시도

{ 
    "207": [ 
     { 
      "order_id": 9325, 
      "other_id": 3332 
     }, 
     { 
      "order_id": 4444, 
      "other_id": 33233 
     } 
    ], 
    "437": [ 
     { 
      "order_id": 9325, 
      "other_id": 22233 
     } 
    ] 
}