Discuz教程网

php简单对象与数组的转换函数代码(php多层数组和对象的转换)

[复制链接]
authicon 星火燎原 发表于 2011-5-18 10:53:24 | 显示全部楼层 |阅读模式
最近用到一些简单的对象与数组的相互转换的问题,写个两个方法如下,需要的朋友可以参考下。

代码如下:
  1. function arrayToObject($e){
  2. if( gettype($e)!='array' ) return;
  3. foreach($e as $k=>$v){
  4. if( gettype($v)=='array' || getType($v)=='object' )
  5. $e[$k]=(object)arrayToObject($v);
  6. }
  7. return (object)$e;
  8. }

  9. function objectToArray($e){
  10. $e=(array)$e;
  11. foreach($e as $k=>$v){
  12. if( gettype($v)=='resource' ) return;
  13. if( gettype($v)=='object' || gettype($v)=='array' )
  14. $e[$k]=(array)objectToArray($v);
  15. }
  16. return $e;
  17. }
复制代码
上面的内容来自 cnblogs jaiho
php多层数组和对象的转换
多层数组和对象转化的用途很简单,便于处理WebService中多层数组和对象的转化
简单的(array)和(object)只能处理单层的数据,对于多层的数组和对象转换则无能为力。
通过json_decode(json_encode($object)可以将对象一次性转换为数组,但是object中遇到非utf-8编码的非ascii字符则会出现问题,比如gbk的中文,何况json_encode和decode的性能也值得疑虑。

下面上代码:
代码如下:
  1. <?php

  2. function objectToArray($d) {
  3. if (is_object($d)) {
  4. // Gets the properties of the given object
  5. // with get_object_vars function
  6. $d = get_object_vars($d);
  7. }

  8. if (is_array($d)) {
  9. /*
  10. * Return array converted to object
  11. * Using __FUNCTION__ (Magic constant)
  12. * for recursive call
  13. */
  14. return array_map(__FUNCTION__, $d);
  15. }
  16. else {
  17. // Return array
  18. return $d;
  19. }
  20. }

  21. function arrayToObject($d) {
  22. if (is_array($d)) {
  23. /*
  24. * Return array converted to object
  25. * Using __FUNCTION__ (Magic constant)
  26. * for recursive call
  27. */
  28. return (object) array_map(__FUNCTION__, $d);
  29. }
  30. else {
  31. // Return object
  32. return $d;
  33. }
  34. }

  35. // Useage:
  36. // Create new stdClass Object
  37. $init = new stdClass;
  38. // Add some test data
  39. $init->foo = "Test data";
  40. $init->bar = new stdClass;
  41. $init->bar->baaz = "Testing";
  42. $init->bar->fooz = new stdClass;
  43. $init->bar->fooz->baz = "Testing again";
  44. $init->foox = "Just test";

  45. // Convert array to object and then object back to array
  46. $array = objectToArray($init);
  47. $object = arrayToObject($array);

  48. // Print objects and array
  49. print_r($init);
  50. echo "\n";
  51. print_r($array);
  52. echo "\n";
  53. print_r($object);
  54. ?>
复制代码



上一篇:php的list()的一步操作给一组变量进行赋值的使用
下一篇:PHP 模拟浏览器 CURL 采集阿里巴巴
authicon haidideyu 发表于 2011-5-18 19:29:44 | 显示全部楼层
这个要顶起来啊
authicon 月之海洋 发表于 2011-5-18 23:29:37 | 显示全部楼层
不错不错,我喜欢
authicon summmer 发表于 2011-5-19 04:29:33 | 显示全部楼层
看看  好像不错
authicon rin421 发表于 2011-5-19 05:30:02 | 显示全部楼层
楼主威武
authicon huanglv80 发表于 2011-5-19 11:29:52 | 显示全部楼层
这个要顶起来啊
authicon lakelouise 发表于 2011-5-19 18:31:15 | 显示全部楼层
顶顶更健康
authicon 纤陌陌 发表于 2011-5-19 21:59:37 | 显示全部楼层
很好 很强大。。谢谢分享
authicon 丁加丁 发表于 2011-5-19 23:29:52 | 显示全部楼层
楼主真强大
authicon №小乖 发表于 2011-5-20 01:29:58 | 显示全部楼层
这个不错呀
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 17:23

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表