There's no way, out-of-the-box, to tell Doctrine to treat an object as an array. However, with a little tweaking and some questionable design decisions, you can achieve this.
The first thing you need to do, is create a base class (for your classes to extend) that implements the ArrayAccess interface. An article describing this can be found in the Doctrine cookbook. It will look something like this:
abstract class DomainObject implements ArrayAccess
{
public function offsetExists($offset)
{
return isset($this->$offset);
}
public function offsetSet($offset, $value)
{
throw new BadMethodCallException(
"Array access of class " . get_class($this) . " is read-only!"
);
}
public function offsetGet($offset)
{
return $this->$offset;
}
public function offsetUnset($offset)
{
throw new BadMethodCallException(
"Array access of class " . get_class($this) . " is read-only!"
);
}
}
Then, when you create your model classes (or at least the ones that you want to treat as arrays), you'll need to extend this DomainObject class. The last piece of the puzzle is to make your class properties public in order to give the json_encode function the ability to inspect your class properties and use them as keys for the json object.
NB: using public properties in classes can lead to a lot of hard to trace bugs and is generally considered questionable practice. this is just an example that i whipped up quickly to illustrate how it could be accomplished. I'm sure there is a more elegant way to implement this that doesn't require public properties. This solution is just meant to get the ball rolling
An example domain class might look something like this:
class Tester extends DomainObject
{
public $foo;
public $bar;
public function __construct($foo, $bar)
{
$this->foo = $foo;
$this->bar = $bar;
}
}
Now you'll be able to cast an instance of the Tester class to an array and pass that array to json_encode:
$test = new Tester('Hello', 'World');
echo json_encode((array)$test);
Which will produce the following output:
{"foo":"Hello","bar":"World"}
EDIT: just to bring your code snippet back into context. You don't need to use HYDRATE_ARRAY anymore, it would just look like this:
$results = $em->getRepository('MyBundle:Report')->findByEvaluation($evaluation_id);
foreach ($results as $result) {
echo json_encode((array)$result);
}
provided your Report class extends the DomainObject class defined above.