PHP 数组与对象相互转换

作者:阿圣(Sfan) 发布于:2009-6-21 3:25 Sunday 分类:Web

看到别人写的程序 有自己写数组与对象转换的函数(obj2arr(),):搜索了一下..才发现原来系统有直接互换的函数..

 不知道有什么弊端..大家要选用自己写的函数..

  1. <?php  
  2. $config = new stdClass();  
  3. $config -> db_host = 'localhost';  
  4. $config -> db_user = 'root';  
  5. $config -> db_pwd = '123456';  
  6. $config -> db_name = 'gbook';  
  7. $config -> webtitle = '留言板';  
  8. $config -> db_prefix = '';  
  9. $config -> isLogin = false;  
  10. $config -> pageSize = 10;  
  11. $config -> filter_word='温家宝,胡景涛,毛泽东';  
  12. $config -> tips=array("欢迎使用本留言板",  
  13.                       "曾经相遇,总胜过从未碰头。",  
  14.                       ) ;  
  15. $config -> a = 'index';  
  16. $config -> v = null;  
  17. $arr=(array)$config;  
  18. print_r($arr);  
  19. $obj=new ArrayObject($arr);  
  20. print_r($obj);  
  21. ?>  
  22.  
  23. 输出:  
  24. Array  
  25. (  
  26.     [db_host] => localhost  
  27.     [db_user] => root  
  28.     [db_pwd] => 123456  
  29.     [db_name] => gbook  
  30.     [webtitle] => 留言板  
  31.     [db_prefix] =>   
  32.     [isLogin] =>   
  33.     [pageSize] => 10  
  34.     [filter_word] => 温家宝,胡景涛,毛泽东  
  35.     [tips] => Array  
  36.         (  
  37.             [0] => 欢迎使用本留言板  
  38.             [1] => 曾经相遇,总胜过从未碰头。  
  39.         )  
  40.  
  41.     [a] => index  
  42.     [v] =>   
  43. )  
  44. ArrayObject Object  
  45. (  
  46.     [db_host] => localhost  
  47.     [db_user] => root  
  48.     [db_pwd] => 123456  
  49.     [db_name] => gbook  
  50.     [webtitle] => 留言板  
  51.     [db_prefix] =>   
  52.     [isLogin] =>   
  53.     [pageSize] => 10  
  54.     [filter_word] => 温家宝,胡景涛,毛泽东  
  55.     [tips] => Array  
  56.         (  
  57.             [0] => 欢迎使用本留言板  
  58.             [1] => 曾经相遇,总胜过从未碰头。  
  59.         )  
  60.  
  61.     [a] => index  
  62.     [v] =>   
  63. )  

 

标签: PHP 对象 数组

评论(0) 引用(0) 浏览(10631)

Powered by emlog 湘ICP备08103487号