只要打字就行了
$array = (array) $yourObject;
从100起:
如果将对象转换为数组,则结果是一个数组,其元素是对象的属性.键是成员变量名,但有几个值得注意的例外:整数属性不可访问;私有变量的类名在变量名前面;受保护变量的变量名前面有一个"*".这些带前缀的值两边都有空字节.
Example: Simple Object
$object = new StdClass;
$object->foo = 1;
$object->bar = 2;
var_dump( (array) $object );
Output:个
array(2) {
'foo' => int(1)
'bar' => int(2)
}
Example: Complex Object
class Foo
{
private $foo;
protected $bar;
public $baz;
public function __construct()
{
$this->foo = 1;
$this->bar = 2;
$this->baz = new StdClass;
}
}
var_dump( (array) new Foo );
Output (with \0s edited in for clarity):个
array(3) {
'\0Foo\0foo' => int(1)
'\0*\0bar' => int(2)
'baz' => class stdClass#2 (0) {}
}
Output with 100 instead of 101:
array (
'' . "\0" . 'Foo' . "\0" . 'foo' => 1,
'' . "\0" . '*' . "\0" . 'bar' => 2,
'baz' =>
stdClass::__set_state(array(
)),
)
这种类型转换不会对对象图进行深度转换,您需要应用空字节(如手册引号中所述)来访问任何非公共属性.因此,当强制转换StdClass对象或仅具有公共属性的对象时,此方法效果最好.又快又脏(你要的东西)没问题.
另请参阅这篇深入的博文: