tonjel
tonjel

Reputation: 99

PHP: Converting curly braces with square braces

I have a problem with JSON format on my controller output. It nothings wrong with it, but I just need to change it into a different form of JSON.

My JSON was like this:

{"data": [{"function_name": "Y", "register_id": "1", "age": 26, "contract_from": "01-07-18", "contract_until": "31-12-99", "worked_hours": 1, "days": 9, "costs": 7, "hourly_rate": 2}]}

and my controller:

public function list_test(){
  $filter = $this->input->post('filter');
  $query  = $this->db->query("select * from hc where contract_from = '".$filter."'")->result();
  $data['data'] = $query;
  print_r(json_encode($data,JSON_PRETTY_PRINT));
}

How could I do to replace curly braces inside data to be swapped with square braces?

My expected output:

{"data": [["Y", "1", "26", "01-07-18", "31-12-99", "1", "9","7", "2"]]}

Is it possible to do that?

Upvotes: 0

Views: 1272

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57131

Not sure if it still is the best result to get (why remove useful information like column names).

You would need to process each row to remove the keys by using array_values()...

$data = ['data' => array_map("array_values", $query) ];

I've also changed the way the $data array is created so that you know it is initialised properly.

Upvotes: 4

Related Questions