2014-03-06 302 views
0

我試圖做一個json,看起來像這樣:族元素

{ 
    "data": { 
     "error_message": "", 
     "data": { 
      "person": [ 
       [ 
        { 
         "name": "teset1", 
         "image": "http://test.co.il/test/icons/billadress.png" 
        }, 
        { 
         "name": "test2", 
         "image": "http://test.co.il/test/icons/email.png" 
        }, 
        { 
         "name": "test3", 
         "image": "http://test.co.il/test/icons/homeicon.png" 
        } 
       ], 
       [ 
        { 
         "name": "a", 
         "image": "http://test.co.il/shay/keykit/icons/billadress.png" 
        }, 
        { 
         "name": "b", 
         "image": "http://test.co.il/shay/keykit/icons/email.png" 
        }, 
        { 
         "name": "c", 
         "image": "http://dvns.co.il/shay/keykit/icons/homeicon.png" 
        } 
       ], 
       [ 
        { 
         "name": "a", 
         "image": "http://test.co.il/test/icons/billadress.png" 
        }, 
        { 
         "name": "b", 
         "image": "http://test.co.il/test/icons/email.png" 
        }, 
        { 
         "name": "c", 
         "image": "http://dvns.co.il/test/icons/homeicon.png" 
        } 
       ], 

      ] 
     } 
    }, 

} 

這是我使用我越來越從MySQL查詢名稱&圖像後的代碼:

$response = $this->_db->query($query)->result_array(); 
$icons_results = array(); 

$obj = new stdclass(); 
foreach ($response as $key => $response) { 
    $icons_model = new icons_model(); 

    $icons_model->init($response); 
    $obj->families[] = $icons_model; 


} 

return $obj; 

所以..這就是我得到:

{ 
    "data": { 
     "families": [ 
      { 
       "id": "1", 
       "name": "a", 
       "image_url": "http://test.co.il/shay/keykit/icons/billadress.png" 
      }, 
      { 
       "id": "2", 
       "name": "b", 
       "image_url": "http://test.co.il/shay/keykit/icons/email.png" 
      }, 
      { 
       "id": "3", 
       "name": "c", 
       "image_url": "http://test.co.il/test/icons/homeicon.png" 
      }, 
      { 
       "id": "6", 
       "name": "f", 
       "image_url": "http://test.co.il/test/icons/homeicon.png" 
      }, 
      { 
       "id": "4", 
       "name": "d", 
       "image_url": "http://test.co.il/test/icons/billadress.png" 
      }, 
      { 
       "id": "5", 
       "name": "e", 
       "image_url": "http://test.co.il/test/icons/billadres2323s.png" 
      }, 

和做。

如何使每三個對象將在一個組? (如第一個例子)

回答

1

嘗試這種情況:

foreach ($response as $key => $response) { 
    $icons_model = new icons_model(); 

    $icons_model->init($response); 
    $obj->families[(int)($key/3)][] = $icons_model; 
}