Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to use data conversion a few days ago, and create a class to convert data.
PHP converts and serializes data into JSON data, and deserializes JSON data. In normal development, arrays and objects are sometimes used for conversion. I used it a few days ago, so we get a class to implement data conversion.
The usage is shown below:
Convert an array to an object:
Public function index (){
Import ('class. object_array ', APP_PATH); // import the paging Class
$ Data = array ('name' => 'wei wei', 'age' => 15, 'sex' => 'male ', 'hobby' => 'favorite Network Worms ');
$ Obj = object_array: get_object ($ data );
P ($ obj );
}
Implementation results:
Convert an object to an array:
Class TestAction extends CommonAction {
Public function index (){
Import ('class. object_array ', APP_PATH); // import the paging Class
$ Holobby ['pingpang '] = new holobby ('table tennis', '2 year ');
$ Holobby ['qifany'] = new holobby ('qifany', '6 year ');
$ Me = new Human ('wei wei', 15, 'mal', $ holobby );
$ Array = object_array: get_array ($ me );
P ($ array );
}
}
/*
Human
*/
Class Human {
Public $ name;
Public $ age;
Public $ sex;
Public $ holobby;
Public function Human ($ name, $ age, $ sex, $ holobby ){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> sex = $ sex;
$ This-> holobby = $ holobby;
}
}
/*
Hobbies
*/
Class holobby {
Public $ name;
Public $ time;
Public function holobby ($ name, $ time ){
$ This-> name = $ name;
$ This-> time = $ time;
}
}
Run:
Hope to help you a little bit, free shoes: to add a little traffic to me: http://wei416978817.byethost33.com Web Pages have updated, please provide suggestions!
Object_array.class.rar (365 B download: 56 times)
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB