问题
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