首页 > Web开发 > 详细

stdobj to array php

时间:2015-12-08 18:33:40      阅读:225      评论:0      收藏:0      [点我收藏+]

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 with json_encode first.

Requirements

The json_encode and json_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
When TRUE, 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);

 

Source page: http://stackoverflow.com/questions/18576762/php-stdclass-to-array

stdobj to array php

原文:http://www.cnblogs.com/Jim-william/p/5029680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!