php对象转为数组的函数是什么
时间:2021-06-15 来源:互联网
今天PHP爱好者给大家带来在php中,对象转为数组的函数是“get_object_vars()”。get_object_vars()函数可以返回由对象属性组成的关联数组,语法格式“get_object_vars (object)”。
本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑
在php中,可以使用get_object_vars()函数来将对象转为数组。
get_object_vars — 返回由对象属性组成的关联数组。语法格式:
get_object_vars ( object $obj ) : array
返回由 obj 指定的对象中定义的属性组成的关联数组。
注意:
在 PHP 4.2.0 之前的版本中,如果在 obj 对象实例中声明的变量没有被赋值,则它们将不会在返回的数组中。而在 PHP 4.2.0 之后,这些变量作为键名将被赋予 null 值。
示例:
<?php
class Point2D {
var $x, $y;
var $label;
function Point2D($x, $y)
{
$this->x = $x;
$this->y = $y;
}
function setLabel($label)
{
$this->label = $label;
}
function getPoint()
{
return array("x" => $this->x,
"y" => $this->y,
"label" => $this->label);
}
}
// "$label" is declared but not defined
$p1 = new Point2D(1.233, 3.445);
print_r(get_object_vars($p1));
$p1->setLabel("point #1");
print_r(get_object_vars($p1));
?>
输出:
Array
(
[x] => 1.233
[y] => 3.445
[label] =>
)
Array
(
[x] => 1.233
[y] => 3.445
[label] => point #1
)
以上就是php对象转为数组的函数是什么的详细内容,更多请关注php爱好者其它相关文章!
-
币安怎么购买First币?First购买教程与币安binance下载入口 2025-05-15
-
币安怎么购买Pay币?Pay购买教程与币安binance下载入口 2025-05-15
-
《“马上就到”的距离单位是什么?》 2025-05-15
-
币安怎么购买Attention币?Attention购买教程与币安binance下载入口 2025-05-15
-
币安怎么购买MATIC币?MATIC购买教程与币安binance下载入口 2025-05-15
-
《为什么我的灵感总是在洗澡的时候出现?》 2025-05-15