AshHimself AshHimself - 1 month ago 16
PHP Question

Remove model name from json results in cakephp 2.x

I am using cakephp 2.x and returning some json data using the following; I would like to remove the model name User from the results so only the key and value is returned.

$this->set('_serialize', 'json');


and my results are returned in the following format;

[
{
"User": {
"fullname": "John Smith"
}
},
{
"User": {
"fullname": "Jiles Smith"
}
}
}
]


I am tried the following;

$json = json_encode(Set::extract('/User/.', $json));


but its returned like;

"[{\"fullname\":\"Ashley Smith\"},{\"fullname\":\"Leigh Smith\"},{\"fullname\":\"Charlie Chocolate\"}]"

Answer

@ndm posted the solution above. I would mark his answer as correct but I am unsure on how to.

Solution was to replace

$json = json_encode(Set::extract('/User/.', $json); 

with

$json = Set::extract('/User/.', $json);
Comments