php+对象+toarray_PHP 对象、数组间的转换

PHP 对象、数组间的转换

/**

* PHP 对象、数组间的转换

*

* @author flyer0126

* @since 2012/05/03

**/

// 1. 利用(array)和(object),简单处理

$objTemp = (object)array();

$objTemp->a = 1;

$objTemp->b = 2;

$objTemp->c = 3;

$arrTemp = (array)$objTemp;

print_r($objTemp);

print_r($arrTemp);

/**

stdClass Object

(

[a] => 1

[b] => 2

[c] => 3

)

Array

(

[a] => 1

[b] => 2

[c] => 3

)

**/

// PS:简单的(array)和(object)只能处理单层的数据,对于多层的数组和对象转换则无能为力。

// 2. 多维数组与对象间的转换处理

/**

* 将对象转换为多维数组

*

**/

function objectToArray($d) {

if (is_object($d)) {

// Gets the properties of the given object

// with get_object_vars function

$d = get_object_vars($d);

}

if (is_array($d)) {

/*

* Return array converted to object

* Using __FUNCTION__ (Magic constant)

* for recursive call

*/

return array_map(__FUNCTION__, $d);

}

else {

// Return array

return $d;

}

}

/**

* 将多维数组转换为对象

*

**/

function arrayToObject($d) {

if (is_array($d)) {

/*

* Return array converted to object

* Using __FUNCTION__ (Magic constant)

* for recursive call

*/

return (object) array_map(__FUNCTION__, $d);

}

else {

// Return object

return $d;

}

}

// Useage:

$init = new stdClass;

$init->foo = "Test data";

$init->bar = new stdClass;

$init->bar->baaz = "Testing";

$init->bar->fooz = new stdClass;

$init->bar->fooz->baz = "Testing again";

$init->foox = "Just test";

// Convert array to object and then object back to array

$array = objectToArray($init);

$object = arrayToObject($array);

// Print objects and array

print_r($init);

print_r($array);

print_r($object);

/**

stdClass Object

(

[foo] => Test data

[bar] => stdClass Object

(

[baaz] => Testing

[fooz] => stdClass Object

(

[baz] => Testing again

)

)

[foox] => Just test

)

Array

(

[foo] => Test data

[bar] => Array

(

[baaz] => Testing

[fooz] => Array

(

[baz] => Testing again

)

)

[foox] => Just test

)

stdClass Object

(

[foo] => Test data

[bar] => stdClass Object

(

[baaz] => Testing

[fooz] => stdClass Object

(

[baz] => Testing again

)

)

[foox] => Just test

)

**/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/291253.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode之Happy Number

1、题目 Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process u…

高效沟通的7C原则

2019独角兽企业重金招聘Python工程师标准>>> 怎样确保沟通的顺畅和高效性呢?成功人士已经总结了很多方法,七项基本原则是一种基本的方法,起到了检查列表的作用,在你发送信息之前,对照检查可以帮助你确认信息…

ps切片导出时将切片选项选择为“所有用户切片”

ps切片导出时将切片选项选择为“所有用户切片”,可导出所有切中的区域。转载于:https://www.cnblogs.com/npk19195global/p/4513707.html

WTMPlus 1.4 Uniapp来了

点击上方蓝字关注我们1.4版本长期以来,WTM都是后台管理系统的开发利器,对于移动端支持的不够。这次WTMPlus 1.4我们加入了对UniApp的支持,你可以轻松的使用WTMPlus同时制作后台管理系统和各种移动端小程序了。前后台模式切换用户现在可以自由…

php真随机数,php 的伪随机数与真随机数实例详解

这篇文章主要介绍了PHP的伪随机数与真随机数详解,本文首先讲解了真随机数和伪随机数的相关概念,并给出了比用mt_rand()函数产生更好的伪随机数的一段例子代码,需要的朋友可以参考下首先需要声明的是,计算机不会产生绝对随机的随机数,计算机只能产生“伪随…

LeetCode之Power of Two

1、题目 Given an integer, write a function to determine if it is a power of two. Credits: Special thanks to jianchao.li.fighter for adding this problem and creating all test cases. Subscribe to see which companies asked this question. 2、分析 比如我们发…

代码的坏味道之一——译自《重构》

重复代码Duplicated Code 臭味集合里面排第一的就是重复代码了。如果你在不止一处发现了同样结构的代码,你可以确定如果你找到一种方法来统一他们的话,你的程序将会改善。 最简单的重复代码问题是当你在同一个类中有两个方法有相同的表达时出现的。那么你…

远程连接mysql速度慢的解决方法

PHP远程连接MYSQL速度慢,有时远程连接到MYSQL用时4-20秒不等,本地连接MYSQL正常,出现这种问题的主要原因是,默认安装的MYSQL开启了DNS的反向解析,在MY.INI(WINDOWS系统下)或MY.CNF(UNIX或LINUX系统下)文件的[mysqld]下加入skip-name-resolve这一句。连接mysql速度慢的解决方法.…

C#中的表达式和运算符

欢迎您成为我的读者,希望这篇文章能给你一些帮助。前言今天和大家一起学习下C#中的表达式和运算符,都是很基础的知识点。在日常的编码过程中,对于表达式和运算符我们每天都在使用。比如像下面的代码int age27;就是一种表达式。运算符是一个符…

【转】jQuery中的bind(),live(),delegate(),on()事件绑定方式的区别

bind() 简要描述 bind()向匹配元素添加一个或多个事件处理器。…

LeetCode之Two Sum II - Input array is sorted

1、题目 Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index…

php 怎么定义一个空对象,php定义空对象的方法

本文主要和大家分享php定义空对象的方法&#xff0c;有时候我们直接对不存在的数组直接定义其下标的值,不会报错,但是我们定义不存在的对象的时候,就会报错,这个时候我们定义一个空对象即可.有以下三种方法:<?php $obj1 new \stdClass;// Instantiate stdClass object$obj…

C#利用Socket实现客户端之间直接通信

2019独角兽企业重金招聘Python工程师标准>>> 实验功能&#xff1a; 设计程序&#xff0c;分别构建通信的两端:服务器端和客户端应用程序,套接字类型为面向连接的Socket,自己构建双方的应答模式&#xff0c;实现双方的数据的发送和接收&#xff08;S发给C&#xff0c…

如何在画面中摆放大量图片

2019独角兽企业重金招聘Python工程师标准>>> 有设计经验的一般都知道&#xff0c;版式设计需要对画面元素之间的关系有充分的认识&#xff0c;并能够在足够有限空间内合理布局&#xff0c;将图形与文字合理结合&#xff0c;下面就来给大家介绍下版式设计的方法。 如…

实现流水灯以间隔500ms的时间闪烁(系统定时器SysTick实现的精确延时)

/** ****************************************************************************** * file main.c * author iuc * version version 1.0 * date 2015-5-19 19:37:52 * brief 流水灯闪烁 ****************************************************************************** …

Android之解决360奇酷手机控制台打印全等级日志(默认只打印W、E等级日志)

1、问题 360奇酷手机很奇葩&#xff0c;默认安卓日志只打印等级W、E,现在我想打印所有等级日志 2、解决办法 1、在桌面点击拨号&#xff0c;在拨号盘输入“*20121220#”&#xff0c;进入工程模式; 2、点击“日志输出等级” 3、选择下面的选项 Log print enable 选 enable J…

php为什么在变量前加,php中变量前加、@等符号是什么意思?

如&#xff1a;$this->config &$config;如&#xff1a;if($_POST[add]){...}还有这个static function &instance() {static $object;if(empty($object)) {$object new self();}return $object;}分别都是什么意思和作用&#xff0c;请说明&#xff0c;感谢&#xf…

几个想法,有兴趣的可以深入下去

这些想法都是不成熟的想法&#xff0c;但是觉得会很有意思&#xff0c;先记于此&#xff0c;有兴趣的可以研究。 转载于:https://www.cnblogs.com/todoit/p/3793887.html

Windows获取CPU、内存和磁盘使用率脚本

获取CPU使用率脚本&#xff08;vbs&#xff09;&#xff0c;另存为cpu.vbs&#xff1a;On Error Resume Next Set objProc GetObject("winmgmts:\\.\root\cimv2:win32_processorcpu0") Wscript.Echo "CPU 使用率: " & objProc.LoadPercentage & …

ASP.NET Core 替换 Action 实际执行方法

RequestDelegate上次&#xff0c;我们在《如何判断当前请求的API类型》中查看endpoints.MapControllers()实现时&#xff0c;最终定位到ActionEndpointFactory.cs&#xff0c;其中有这样一段代码&#xff1a;private static RequestDelegate CreateRequestDelegate() {// We do…