Home > php教程 > php手册 > 对象数组的转化

对象数组的转化

WBOY
Release: 2016-06-07 11:43:28
Original
998 people have browsed it

前几天用到了数据的转化,弄了一个类进行实现数据的转换
PHP中有将数据转换序列化成JSON数据的,也有反序列化JSON数据的,正常的开发中有时会使用到数组和对象之间的转换,前几天我就用到了,于是就弄了一个类实现数据的转换。
下面贴出用法:
数组转换成对象:public function index(){<br>         import('Class.object_array',APP_PATH);// 导入分页类<br> <br>         $data = array('name' =>'魏巍' ,'age'=>15,'sex'=>'男','hobby'=>'喜欢起凡的网虫' );<br>         $obj = object_array::get_object($data);<br>         p($obj);<br>     }实现的效果:
对象数组的转化
对象转换成数组:class TestAction extends CommonAction{<br> <br>     public function index(){<br>         import('Class.object_array',APP_PATH);// 导入分页类<br> <br>         $hobby['pingpang']=new Hobby('乒乓球','2年');<br>                 $hobby['qifan']=new Hobby('起凡','6年');<br>                 $me=new Human('魏巍',15,'男',$hobby);<br>                 $array = object_array::get_array($me);<br>                 p($array);<br>     }<br> }<br> /*<br>     人类<br> */<br> class Human {<br>     public $name;<br>     public $age;<br>     public $sex;<br>     public $hobby;<br>     public function Human($name,$age,$sex,$hobby) {<br>         $this->name  = $name;<br>         $this->age   = $age;<br>         $this->sex   = $sex;<br>         $this->hobby = $hobby;<br>     }<br> }<br> /*<br>     爱好类<br> */<br> class Hobby{<br>     public $name;<br>     public $time;<br>     public function Hobby($name,$time){<br>         $this->name=$name;<br>         $this->time=$time;<br>     }<br> }运行效果图:
对象数组的转化
希望可以帮助到您一点点,有空的童鞋:去给我增加一点流量:http://wei416978817.byethost33.com Web Pages有更新,请提供建议!

附件 object_array.class.rar ( 365 B 下载:56 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template