PHP 数组与对象相互转换
作者:阿圣(Sfan) 发布于:2009-6-21 3:25 Sunday 分类:Web
看到别人写的程序 有自己写数组与对象转换的函数(obj2arr(),):搜索了一下..才发现原来系统有直接互换的函数..
不知道有什么弊端..大家要选用自己写的函数..
- <?php
- $config = new stdClass();
- $config -> db_host = 'localhost';
- $config -> db_user = 'root';
- $config -> db_pwd = '123456';
- $config -> db_name = 'gbook';
- $config -> webtitle = '留言板';
- $config -> db_prefix = '';
- $config -> isLogin = false;
- $config -> pageSize = 10;
- $config -> filter_word='温家宝,胡景涛,毛泽东';
- $config -> tips=array("欢迎使用本留言板",
- "曾经相遇,总胜过从未碰头。",
- ) ;
- $config -> a = 'index';
- $config -> v = null;
- $arr=(array)$config;
- print_r($arr);
- $obj=new ArrayObject($arr);
- print_r($obj);
- ?>
- 输出:
- Array
- (
- [db_host] => localhost
- [db_user] => root
- [db_pwd] => 123456
- [db_name] => gbook
- [webtitle] => 留言板
- [db_prefix] =>
- [isLogin] =>
- [pageSize] => 10
- [filter_word] => 温家宝,胡景涛,毛泽东
- [tips] => Array
- (
- [0] => 欢迎使用本留言板
- [1] => 曾经相遇,总胜过从未碰头。
- )
- [a] => index
- [v] =>
- )
- ArrayObject Object
- (
- [db_host] => localhost
- [db_user] => root
- [db_pwd] => 123456
- [db_name] => gbook
- [webtitle] => 留言板
- [db_prefix] =>
- [isLogin] =>
- [pageSize] => 10
- [filter_word] => 温家宝,胡景涛,毛泽东
- [tips] => Array
- (
- [0] => 欢迎使用本留言板
- [1] => 曾经相遇,总胜过从未碰头。
- )
- [a] => index
- [v] =>
- )