How do I json_encode a Kohana ORM Result as an array

August 31, 2011

I’m working on an Kohana based API where I needed to return some data from an ORM result as an array. Sure, I can loop through the ORMIterator and assign the results to an array, but there must be a nicer way of doing this. I ran across this thread which uses and anonymous function to do the as_array() call. See:

echo json_encode( array_map( create_function( '$obj', 'return $obj->as_array();', ORM::factory('image')->find_all()->as_array() ) );

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive