向数组元素添加双引号

时间:2015-05-21 02:51:38

标签: php arrays json

我使用的是Datatables JQuery插件,需要用双引号括起数据元素。

Array
(
    [0] => ticket #6,2015-05-20T19:36:02Z,open,normal,34
    [1] => testing org,2015-05-15T19:20:11Z,closed,,19
    [2] => testing ticket,2015-05-20T19:29:09Z,open,normal,29
    [3] => testing ticket #2 ,2015-05-20T19:30:55Z,open,normal,30
    [4] => ticket #3,2015-05-20T19:33:25Z,open,normal,31
    [5] => ticket #4,2015-05-20T19:34:32Z,open,normal,32
    [6] => ticket #5,2015-05-20T19:35:03Z,open,normal,33
)

我尝试使用内爆函数,但没有成功,我希望有人能帮助我完成这项任务

期望的输出:

"ticket #6","2015-05-20T19:36:02Z","open","normal","34"

这样当使用json_encode时,它将被打印出来。

{
  "data": [
    [
       "ticket #6",
       "2015-05-20T19:36:02Z",
       "open",
       "normal",
       "34"

    ]

  ]
}

1 个答案:

答案 0 :(得分:2)

这应该适合你:

只需使用array_map()explode()来检查所有数组元素,例如

$result = array_map(function($v){
    return explode(",", $v));
}, $arr);

因此,使用json_encode(),您最终会得到:

[
  ["ticket #6","2015-05-20T19:36:02Z","open","normal","34"],
  ["testing org","2015-05-15T19:20:11Z","closed","","19"],
  ["testing ticket","2015-05-20T19:29:09Z","open","normal","29"],
  ["testing ticket #2 ","2015-05-20T19:30:55Z","open","normal","30"],
  ["ticket #3","2015-05-20T19:33:25Z","open","normal","31"],
  ["ticket #4","2015-05-20T19:34:32Z","open","normal","32"],
  ["ticket #5","2015-05-20T19:35:03Z","open","normal","33"]
]