简体   繁体   English

PHP:用方括号转换花括号

[英]PHP: Converting curly braces with square braces

I have a problem with JSON format on my controller output.我的 controller output 上的 JSON 格式有问题。 It nothings wrong with it, but I just need to change it into a different form of JSON.它没有错,但我只需要将它更改为 JSON 的不同形式。

My JSON was like this:我的 JSON 是这样的:

{"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:和我的 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:我预期的 output:

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

Is it possible to do that?有可能这样做吗?

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() ...您需要使用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.我还更改了$data数组的创建方式,以便您知道它已正确初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM