sex = "nan";$obj->age = 12;$obj->address = "lihua in hangh"/> sex = "nan";$obj->age = 12;$obj->address = "lihua in hangh">

Home  >  Article  >  Backend Development  >  php深入学习笔记1(数组与对象之间的相互转化)

php深入学习笔记1(数组与对象之间的相互转化)

WBOY
WBOYOriginal
2016-06-13 12:20:03959browse

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);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn