Customizing JSON output CakePHP
I don't fully understand what you mean by "remove the entire array wrapped in "users"" and "remove every object being a member of "User"", but according to your desired output format example, you'll need to extract and pass the exact data that you want to be encoded to json_encode
, instead of passing everything using compact
.
Extracting could be done with the Set
or the Hash
class (depending on your Cake version)
Assuming your model returns the data in the default CakePHP format, this for example:
json_encode(Set::extract('/User/.', $users));
should give you a structure like this:
[{ "user_id": "2", "email": "email@test.com", "name": "Blah"}]
and with multiple users it should look like this
[{ "user_id": "1", "email": "foo@test.com", "name": "Bar"},{ "user_id": "2", "email": "email@test.com", "name": "Blah"}]
Use like this:
$users= (Set::extract('/User/.', $users));pr($users);
It will remove Model
from result Array and then json_encode
or whatever further usage.
More library functions Set class Here and Hash class Here