CakePHP Hash extract example

Hash is a core CakePHP class that can be called from any model or controller in your CakePHP application and will help you to manage your arrays, essential to return well JSON data. These days I am working on generating an API for a iOS application and I was really struggling about how should I make the array formatting so when you call json_encode() it will be formatted just as I want it to.

And then came Hash (Set for <v2) class to save my life.  In my case I had a typical find query on a class that has a many relationship with another class. But I just wanted to format it because the way CakePHP shows arrays is just ugly for a json (at least under my view).


Another interesting methods of Hash class are merge, combine, get, insert, diff, contains, filter, expand and a lot more, you can check them all in the official documentation.