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

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

WBOY
Release: 2016-06-23 13:34:29
Original
855 people have browsed it

//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);
Copy after login

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template