Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php对象转数组方法

php对象转数组方法

发布时间:2020-05-18   编辑:www.jquerycn.cn
jquery中文网为您提供php对象转数组方法等资源,欢迎您收藏本站,我们将为您提供最新的php对象转数组方法资源

php对象转数组方法

在php中将对象转数组的方法,可以通过使用“get_object_vars()”函数来实现,该函数的语法为“get_object_vars($obj)”,其参数$obj表示为需要转换的对象,该函数返回值为对象属性组成的关联数组。

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对象转数组方法的详细内容,更多请关注jquery中文网其它相关文章!

  • 本文原创发布jQuery中文网,转载请注明出处,感谢您的尊重!
  • 您可能感兴趣的文章:
    PHP 数组字符集编码转换的函数
    php对象转数组方法
    在PHP中处理JSON数组以及对象
    php将对象转换为数组的函数(递归方法)
    php入门教程(索引)
    php将对象转换为数组函数的代码
    对象串行化
    php面试题目 面向中等水平的程序员
    php和net有什么区别
    php为js数组赋值方法

    [关闭]