前几天用到了数据的转化,弄了一个类进行实现数据的转换
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元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools