this question seems to come up a lot in google and i still dont seem to get a grasp on how to do this.
i am doing a fetchAll and i get back either a object or a array of objects:
if i var_dump it i get:
array(3) {
[0] object(Model_Model)#163 (55) {
['_name':protected] = NULL
['_info1':protected] = NULL
}
[1] object(Talents_Model)#172 (55) {
['_name':protected] = NULL
['_info1':protected] = NULL
}
[2]object(Talents_Model)#143 (55) {
['_name':protected] = NULL
['_info1':protected] = NULL
}
}
if i do $this->_helper->json( $the_object );orjson_encode` i get empty json objects
[{},{},{},{}]
is there a way of converting those objects into json directly, no matter if there is one object or a array of them?
thanks
i write something that solver this problem:
public static function getProperties($object)
{
$array = array();
$reflection = new ReflectionObject($object);
foreach($reflection->getProperties(ReflectionProperty::IS_PROTECTED) as $property)
{
$property->setAccessible(TRUE);
if(!$property->isStatic())
{
$array[preg_replace('/_/', '', $property->getName(), 1)] = $property->getValue($object);
}
}
if(empty($array)) return;
return $array;
}
this method can be changed to be a bit more generic and i also use reflections new in PHP 5.4