Heim >Backend-Entwicklung >PHP-Tutorial >php深入学习笔记一(数组与对象之间的相互转化)
//1. 对象的遍历$obj = new stdClass();$obj->name = "lihua";$obj->sex = "nan";$obj->age = 12;$obj->address = "lihua in hanghzou!";foreach($obj as $v){ echo $v."<br>";}/* lihua nan 12 lihua in hanghzou!*///2.对象转数组//2.1.第一种方式$user = (array)$obj;var_dump($user);//2.2.第二种方式//简单而暴力的转换 只对public属性 有效//第二个参数 为 true 返回 数组// 为false 返回 对象$array = json_decode(json_encode($nested_object), true);//3.数组转对象(注意是关联数组)$user = (object)$user;var_dump($user);