0

I have a question about formating a json in php. Here is my code

public function testing() {

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());

}

With my current simple php code, I'm just getting everything as a JSONArray.

[

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

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

]

But I want to format it on my own way, something like this...Hope you guys help. Thank you in advanced!

{

"Answer": {

    "Success": "Yup"
          },

"List": [

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

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

}

0

2 Answers 2

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

Will print:

{"Answer":{"Sucess":"Yup"},"List":[{"id":1,"Description":"hello"}]}
Sign up to request clarification or add additional context in comments.

Comments

1

try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.