How to remove key from json response?

こ雲淡風輕ζ 提交于 2019-12-11 23:58:21

问题


I've got a collection $tree that is being converted to json with Response::json(). The key is being automatically set to the id of the first object. Is there a way to remove this, or restructure with:

'data': [
    { first object },
    { second object}
]

It is currently responding with:

{"1":{"id":"1","parent_id":null,"lft":"1","rgt":"8","depth":"0"...

When I want is:

{'data':[{"id":"1","parent_id":null,"lft":"1","rgt":"8","depth":"0"...

回答1:


Try to use: Response::json(array( 'data' => array_values($yourDataArray) ) );




回答2:


use :

Response::json(array( 'data' => array_values($dataArray) ) );



回答3:


Well, in PHP it would be easy, just use array_values() on the initial array so that it 'forgets' the array indexes (which by the way, is what 'RootName_X' is called in your case:

 $newvalue = array_values( (array)$value );
echo json_encode($newvalue);


来源:https://stackoverflow.com/questions/29257260/how-to-remove-key-from-json-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!