PHP stdClass的数组 [英] php stdClass to array
问题描述
我有一个对象stdClass已经转换为数组的一个问题。
我曾经这样尝试:
收益率(阵列)$预订;
或
收益率(阵列)json_de code($预订,真正的);
或
收益率(阵列)json_de code($预订);
剧组之前该数组是充满一个记录,经过我尝试将它转换为空。
如何投/转换它而不删除其行?
阵列投前:
阵列(1){[0] =>对象(stdClass的)#23(36){[身份证] =>字符串(1)2[名称] =>字符串(0)[code] =>串(5)56/13}}
在投是空NULL,如果我尝试做一个的var_dump($预订);
我自己也尝试这个功能,但总是空空的:
公共职能objectToArray($ D){
如果(is_object($ D)){
//获取给定对象的属性
//与get_object_vars功能
$ D = get_object_vars($ D);
} 如果(is_array($ D)){
/ *
*返回数组转换为对象
*使用__FUNCTION__(魔术常数)
*对于递归调用
* /
返回array_map(__ FUNCTION__,$ D);
}
其他{
//返回数组
返回$ d个;
}
}
的懒的单行法
最有可能的,因为PHP是在调用速度慢 -您可以在单行使用JSON的方法,如果你愿意失去的性能一点点(尽管有些报道它是不是通过迭代的对象更快递归做到这一点功能)。 但我已经这样做的你说。不完全是 - 您使用 json_de code
上数组,但你需要连接code将其与 json_en code
第一。
要求
json_en code
和< A HREF =http://www.php.net/json_de$c$c> json_de code
方法。这些在PHP 5.2.0及以上自动捆绑。如果您使用旧版本也有一个 PECL库(这么说,在这种情况下,你应该的真正更新你的PHP安装。5.1支持在2006年停止。)
转换的阵列
/ stdClass的
- > stdClass的
$ stdClass的= json_de code(json_en code($预订));
转换的阵列
/ stdClass的
- > 阵列
手动指定 json_de code
的第二个参数为:
assoc命令结果
的当TRUE
,返回的对象将转换成关联数组。的
块引用>因此,将整个事情转换为数组:
$阵列= json_de code(json_en code($预订),TRUE);
I have a problem to convert an object stdClass to array. I have tried in this way:
return (array) $booking;
or
return (array) json_decode($booking,true);
or
return (array) json_decode($booking);
The array before the cast is full with one record, after my try to cast it is empty. How to cast / convert it without delete its rows?
array before cast:
array(1) { [0]=> object(stdClass)#23 (36) { ["id"]=> string(1) "2" ["name"]=> string(0) "" ["code"]=> string(5) "56/13" } }
after cast is empty NULL if I try to make a
var_dump($booking);
I have also tried this function but always empty:
public function objectToArray($d) { if (is_object($d)) { // Gets the properties of the given object // with get_object_vars function $d = get_object_vars($d); } if (is_array($d)) { /* * Return array converted to object * Using __FUNCTION__ (Magic constant) * for recursive call */ return array_map(__FUNCTION__, $d); } else { // Return array return $d; } }
解决方案The lazy one-liner method
You can do this in a one liner using the JSON methods if you're willing to lose a tiny bit of performance (though some have reported it being faster than iterating through the objects recursively - most likely because PHP is slow at calling functions). "But I already did this" you say. Not exactly - you used
json_decode
on the array, but you need to encode it withjson_encode
first.Requirements
The
json_encode
andjson_decode
methods. These are automatically bundled in PHP 5.2.0 and up. If you use any older version there's also a PECL library (that said, in that case you should really update your PHP installation. Support for 5.1 stopped in 2006.)
Converting an
array
/stdClass
->stdClass
$stdClass = json_decode(json_encode($booking));
Converting an
array
/stdClass
->array
The manual specifies this second parameter of
json_decode
as:assoc
WhenTRUE
, returned objects will be converted into associative arrays.Hence it will convert the entire thing into an array:
$array = json_decode(json_encode($booking), true);
这篇关于PHP stdClass的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!