您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php怎么将object转化为数组

2024/4/14 15:10:06发布14次查看
php将object转化为数组的方法:1、利用强制类型转换,语法“(array)object变量”;2、利用get_object_vars()函数,语法“get_object_vars (object变量)”。
本教程操作环境:windows7系统、php7.1版,dell g3电脑
方法1:利用强制类型转换--在要转换的变量之前加上用括号括起来的目标类型“(array)”
示例:object强制类型转换为array
<?phpclass foo{ function do_foo() { echo "doing foo."; }}$bar = new foo;$bar->do_foo();print_r((array)$bar);?>
输出:
doing foo.array ( )
扩展资料:
允许转换的php数据类型有:
(int)、(integer):转换成整形
(float)、(double)、(real):转换成浮点型
(string):转换成字符串
(bool)、(boolean):转换成布尔类型
(array):转换成数组
(object):转换成对象
方法2:使用get_object_vars()函数
get_object_vars — 返回由对象属性组成的关联数组。语法格式:
get_object_vars ( object $obj )
返回由 obj 指定的对象中定义的属性组成的关联数组。
示例:
<?phpclass 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怎么将object转化为数组的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product