2014-08-31 4 views
0

PHP에서 json을 형성하는 것에 대한 질문이 있습니다. 여기에 내 코드형식 JSON PHP from MySQL Query

공공 기능 테스트() 내 현재 간단한 PHP 코드와 {

if (!empty($_POST)) { 
    $this->db->select('*'); 
    $this->db->from('trash_table'); 
    $this->db->where('Description',$_POST['Descp']); 
    $q = $this->db->get(); 

    if($q->num_rows() > 0)  
    //here should be something better 
    print json_encode($q->result()); 

}

, 난 그냥 JSONArray로 모든 것을 얻고있다.

[

{"ID":1,"Description":"hello", 

    {"ID":2,"Description":"hellou"} 

]

는 그러나 나는 내 자신의 길을 포맷하려면, 이런 일이 ... 너희들은 도움 바랍니다. 미리 감사드립니다!

{

"Answer": { 

    "Success": "Yup" 
      }, 

"목록": [

{"ID":1,  
    "Description":"hello"}, 

    {"ID":2,  
    "Description":"hellou"}] 

}

답변

1
$result = array(
    'Answer' => array('Sucess'=>'Yup'), 
    'List' => array(
    array('id' => 1, 'Description' => 'hello'), 
) 
); 
print json_encode($result); 

인쇄 :

{"Answer":{"Sucess":"Yup"},"List":[{"id":1,"Description":"hello"}]} 
+0

작동 확인 :) 정말 감사합니다! –

1

이 시도 :

$list = $q->result(); $result = array("Answer" => array ("success" => "Yup"), "List" => $list ); print json_encode($result);