2014-05-14 3 views
-2

나는 이미 답변을 얻은 배열로 작업하고 있지만 배열에 1 개의 배열 레이어를 추가해야합니다. 이 내 배열배열을 하위 배열로 만드는 방법은 무엇입니까?

: 다음은 내 예입니다

$newOptions = array(); 
    foreach ($community_comment as $option) { 
     $date = $option['date']; 
     $text = $option['text']; 

     $newOptions[$date][] = $text; 
    } 

이 내 결과입니다

Array 
(
    [2014-05-14] => Array 
     (
      [0] => test test test test 
      [1] => test2 
     ) 

) 

하지만 내 결과는 다음과 같이 할 것을 권장합니다

Array 
    (
     [0]=>Array(
      [2014-05-14] => Array 
      (
       [0] => test test test test 
       [1] => test2 
      ) 
    ) 
    ) 

희망을 좀 얻을 수 있습니다. 고맙습니다.

+0

'$ newOptions = array ($ newOptions,)'를 추가하십시오. – Holt

+0

시도해보십시오 $ newOptions [] [$ date] [] –

답변

0

이 당신을 위해 무엇을해야 : 내 대답이를 발견했다
$newOptions[$date][] = $text;
$newOptions[][$date][] = $text;

0

당신은 단지 변경해야 대답은 구현하기에 적합하다. 멘토.

foreach ($community_comment as $option) { 
     $date = $option['date']; 
     $text = $option['text']; 
     $id = $option['id']; 

     $data['text'] = $text; 
     $data['id'] = $id; 
     $newOptions[$date][] = $data; 
    } 

    $result = array(); 
    foreach ($newOptions as $key=>$value) 
    { 
     $result['date'] = $key; 
     $result['data'] = $value; 

     $result2[]=$result; 
    } 
0
$i = 0; 
$fake_array = array(); 
foreach ($community_comment as $option) { 
    $date = $option['date']; 
    $text = $option['text']; 

    if(isset($fake_array[$date])) { 
     $newOptions[$fake_array[$date]][$date][] = $text; // $fake_array[$date] = old index 
    } 
    else { 
     $newOptions[$i][$date][] = $text; 
     $fake_array[$date] = $i; 
    } 
    $i++; 
} 
0
$newOptions = array(); 
    foreach ($community_comment as $option) { 
     $date = $option['date']; 
     $text = $option['text']; 

     $newOptions[][$date][] = $text; 
    } 
0


$newOptions = array(); 
foreach ($community_comment as $option) { 
    $date = $option['date']; 
    $text = $option['text']; 

    $newOptions[][$date][] = $text; 
} 
관련 문제