Another answer to this is to use ArrayObject instead of [] when initialising the variable.
The following illustrates the problem. When the associative array is empty, it is JSON encoded as '[]':
$object = [];
echo json_encode($object); // you get '[]'
$object['hello'] = 'world';
echo json_encode($object); // you get '{hello: "world"}'
Instead, keep everything the same, but when you declare the $object, use ArrayObject:
$object = new ArrayObject;
echo json_encode($object); // you get '{}'
$object['hello'] = 'world';
echo json_encode($object); // you get '{hello: "world"}'
The reason this is a good alternative is because using (object) [] converts the variable to an stdClass meaning you have to access the properties using $variable->property. However, if all your existing code was written using $variable['property'], then using (object) [] means you will have to change all of that code, when you can instead just use new ArrayObject.
Link to ArrayObject that has been available since PHP5