PHP如何将数组转化为对象

时间:2017-07-28
本文章向大家介绍php如何将数组转化为对象,需要的朋友可以参考一下。

我怎样才能将这样的数组转换为对象?

  [128] => Array
        (
            [status] => Figure A.
 Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution.
        )

    [129] => Array
        (
            [status] => The other day at work, I had some spare time
        )

)

这里有三种方法:一、伪造一个真实的物体:

class convert
{
    public $varible;

    public function __construct($array)
    {
        $this = $array;
    }

    public static function toObject($array)
    {
        $array = new convert($array);
        return $array;
    }
}

二、通过将数组转换为对象将数组转换为对象:

$array = array(
    // ...
);
$object = (object) $array;


三、手动将数组转换为对象:

$object = object;
foreach ($arr as $key => $value) {
    $object->{$key} = $value;
}