2

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

2 Answers 2

3
$result=$this->fetchAll($select);
$result=$result->toArray();

I think, then you should use json_encode

Sign up to request clarification or add additional context in comments.

3 Comments

WHERE IS THE METHOD toArray() ?
toArray is a zend method framework.zend.com/manual/en/…
looks like is deprecated, but could be usefull
2

Your real issue is not the JSON conversion, is that the object members are not public!

You should be able to work around this easily if you know the attribute names, or with a bit more work if you don't (using reflection, for instance).

1 Comment

in my setup the properties being protected are not a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.