gpt4 book ai didi

php - 如何使用 zend 框架转换 json 数据中的对象?

转载 作者:行者123 更新时间:2023-12-02 22:40:30 24 4
gpt4 key购买 nike

这个问题似乎在谷歌上出现了很多,但我似乎仍然没有掌握如何做到这一点。

我正在执行一个fetchAll,然后我返回一个对象或一个对象数组:

如果我 var_dump 它我得到:

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

}

}

如果我做 $this->_helper->json( $the_object );orjson_encode` 我得到空的 json 对象 [{},{},{},{}]

有没有一种方法可以将这些对象直接转换为 json,无论是一个对象还是一个对象数组?

谢谢

我写了一些东西来解决这个问题:

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;
}

可以将此方法更改为更通用一些,我还使用 PHP 5.4 中新的 reflections

最佳答案

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

我想,那你应该使用json_encode

关于php - 如何使用 zend 框架转换 json 数据中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10841368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com