我有这个数组
array:82 [
1000 => array:10 [
"id" => 1000
"name" => "name"
"name2" => "name2"
"name3" => "name3"
],
....
]
如何将数组转换为这种json格式?
{
"data": [
{
"id": "1000",
"name1": "name",
"name2": "name2",
"name3": "name3"
},
...
]
}
我尝试这个但不能正常工作
$tableData = array();
$tableData['data'] = array_map(function($session) {
return [$session['id'],$session['name'],$session['name2']];
}, $response);
return response()->json($tableData);
有人有想法吗?
我需要这个用于数据表响应
答案 0 :(得分:1)
$tableData = array();
foreach($session as $sess){
$tableData['data'][] = ["id"=>$sess['id'],"name"=>$sess['name'],"name2"=>$sess['name2'],"name3"=>$sess['name3']];
}
return json_encode($tableData);
答案 1 :(得分:0)
尝试使用json_encode()
功能
http://php.net/manual/en/function.json-encode.php