遍历对象php的所有属性 [英] Looping through all the properties of object php

查看:113
本文介绍了遍历对象php的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何遍历对象的所有属性?现在,我必须编写新的代码行来打印对象的每个属性

How can I loop through all the properties of object?. Right now I have to write a new code line to print each property of object

echo $obj->name;
echo $obj->age;

我可以使用foreach循环或任何循环遍历对象的所有属性吗?

Can I loop through all the properties of an object using foreach loop or any loop?

类似这样的东西

foreach ($obj as $property => $value)  

推荐答案

如果这仅用于调试输出,则还可以使用以下内容查看所有类型和值.

If this is just for debugging output, you can use the following to see all the types and values as well.

var_dump($obj);

如果您想对输出进行更多控制,可以使用以下方法:

If you want more control over the output you can use this:

foreach ($obj as $key => $value) {
    echo "$key => $value\n";
}

这篇关于遍历对象php的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆