找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1903|回复: 0

[教程] php多层数组和对象的转换

[复制链接]
发表于 2013-4-2 20:41:43 | 显示全部楼层 |阅读模式 来自 广东省揭阳市
多层数组和对象转化的用途很简单,便于处理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 '
  51. ';
  52.         print_r($array);
  53.         echo '
  54. ';
  55.         print_r($object);
  56. ?>
复制代码

评分

参与人数 1金币 +5 收起 理由
猫性男孩 + 5 很给力!

查看全部评分

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-6-11 13:36 , Processed in 0.412608 second(s), 9 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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