前面整理过一篇文章,描述php中的array与json的array和object的转换关系。http://www.cnblogs.com/x3d/p/php-json-array-object-type.html

在实际开发中,如何保障这种关系呢?一般来说,需要定义一些类型来做映射。

废话不多说,上码。

1. 最终用法

use hybrid\one\collection\JSONArray;

// json

$arr = new JSONArray();

//$arr[2] = 444; // 异常 不是从0开始

$arr[0] = 1;
$arr[1] = 2;
$arr[2] = 3; var_dump($arr->__toArray()); $arr[6] = 66; // 异常 索引跳跃
$arr['91'] = 91; // 异常 不是整型

2. 相关基础类


<?php namespace hybrid\one\collection; use ArrayIterator;
use IteratorAggregate;
use ArrayAccess;
use Countable; use hybrid\one\Collection; /**
* PHP的数组结构灵活,但在输出JSON数据时往往造成混乱,针对JSON内置的数据集合类型映射对应的PHP类
* JSONArray的特征:
* 数据的下标:
* 必须是数字索引,
* 必须从0开始,
* 必须从小到大依次增加、中间不可以跳跃、顺序不可变动
*/
class JSONArray implements Collection, ArrayAccess, Countable, IteratorAggregate
{
/**
*
* @var array
*/
private $data; public function __toArray(): array
{
return $this->data;
} /**
* Returns an iterator for traversing the data.
* This method is required by the SPL interface [[\IteratorAggregate]].
* It will be implicitly called when you use `foreach` to traverse the collection.
* @return \ArrayIterator an iterator for traversing the cookies in the collection.
*/
public function getIterator()
{
return new ArrayIterator($this->data);
} /**
* Returns the number of data items.
* This method is required by Countable interface.
* @return integer number of data elements.
*/
public function count()
{
return count($this->data);
} /**
*
* @param mixed $offset
* @return boolean
*/
private function offsetLegal($offset)
{
return is_int($offset);
} /**
* This method is required by the interface [[\ArrayAccess]].
* @param int $offset the offset to check on
* @return boolean
*/
public function offsetExists($offset)
{
if (!$this->offsetLegal($offset)) {
throw new IllegalOffsetException;
}
return isset($this->data[$offset]);
} /**
* This method is required by the interface [[\ArrayAccess]].
* @param integer $offset the offset to retrieve element.
* @return mixed the element at the offset, null if no element is found at the offset
*/
public function offsetGet($offset)
{
if (!$this->offsetLegal($offset)) {
throw new IllegalOffsetException;
} return isset($this->data[$offset]) ? $this->data[$offset] : null;
} /**
* This method is required by the interface [[\ArrayAccess]].
* @param integer $offset the offset to set element
* @param mixed $item the element value
*/
public function offsetSet($offset, $item)
{
if (!$this->offsetLegal($offset)) {
throw new IllegalOffsetException;
} if (!$this->offsetExists($offset) && ($offset > $this->count())) {
throw new IllegalOffsetException('offset value is illegal, must be ordered integer value or existed offset given');
} $this->data[$offset] = $item;
} /**
* This method is required by the interface [[\ArrayAccess]].
* @param mixed $offset the offset to unset element
*/
public function offsetUnset($offset)
{
if (!$this->offsetLegal($offset)) {
throw new IllegalOffsetException;
} unset($this->data[$offset]);
} }

3. 其它

JSONObject 类可以依葫芦画瓢。

PHP中模拟JSONArray的更多相关文章

  1. 如何在C#中模拟C++的联合(Union)?[C#, C++] How To Simulate C++ Union In C#?

    1 什么是联合? 联合(Union)是一种特殊的类,一个联合中的数据成员在内存中的存储是互相重叠的.每个数据成员都在相同的内存地址开始.分配给联合的存储区数量是“要包含它最大的数据成员”所需的内存数. ...

  2. Python中模拟enum枚举类型的5种方法分享

    这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下   以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...

  3. 在C#中模拟Javascript的setTimeout方法

    在C#中模拟Javascript的setTimeout方法 背景 每种语言都有自己的定时器(Timer),很多人熟悉Javascript中的setInterval和setTimeout,在Javasc ...

  4. iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。

    转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...

  5. 在SoapUI中模拟用户操作

    SoapUI作为一款接口测试工具,具有极大的灵活性和拓展性.它可以通过安装插件,拓展其功能.Selenium作为一款Web自动化测试插件可以很好的与SoapUI进行集成.如果要在SoapUI中模拟用户 ...

  6. C#7.2——编写安全高效的C#代码 c# 中模拟一个模式匹配及匹配值抽取 走进 LINQ 的世界 移除Excel工作表密码保护小工具含C#源代码 腾讯QQ会员中心g_tk32算法【C#版】

    C#7.2——编写安全高效的C#代码 2018-11-07 18:59 by 沉睡的木木夕, 123 阅读, 0 评论, 收藏, 编辑 原文地址:https://docs.microsoft.com/ ...

  7. php中模拟多继承如何实现

    php中模拟多继承如何实现 一.总结 一句话总结:其实你继承别人也是想调用别人类里面的方法和属性,所以可以这样做:这本类中创建目标类的对象,然后通过这个对象来调用方法和属性,这样比继承来的方便. 二. ...

  8. .net中模拟键盘和鼠标操作

    原文:.net中模拟键盘和鼠标操作 周银辉 其实SendKeys类提供的方法蛮好用的,可惜的是WPF中不能用了,说是WPF的消息循环方式改成了Dispatcher,所以直接调用System.Windo ...

  9. CSS中模拟父元素选择器

    很多情况下,我们需要找到父元素,但可惜的是css中并没有这样的一个选择器. 至于原因可以看张鑫旭的如何在CSS中实现父选择器效果这篇文章. 简单来说这个实现并不是真正的父元素选择器,只是利用其它思路来 ...

随机推荐

  1. windows环境redis主从安装部署

    准备工作 下载windows环境redis,我下载的是2.4.5,解压,拷贝一主(master)两从(slaveof).主机端口使用6379,两从的端口分别为6380和6381, 我本地索性用6379 ...

  2. BPM配置故事之案例4-子表

    公司渐渐对采购管理重视起来了,新招聘了采购主管老李,老李对现有的申请表很不满意,要求将申请物资和申请原因改成物资明细表 物资明细表 小明只好继续致电大毛-- 大毛:把申请物资和申请原因删掉,新增一个数 ...

  3. iOS开发 判断当前APP版本和升级

    从iOS8系统开始,用户可以在设置里面设置在WiFi环境下,自动更新安装的App.此功能大大方便了用户,但是一些用户没有开启此项功能,因此还是需要在程序里面提示用户的 方法一:在服务器接口约定对应的数 ...

  4. 浅谈SQL注入风险 - 一个Login拿下Server

    前两天,带着学生们学习了简单的ASP.NET MVC,通过ADO.NET方式连接数据库,实现增删改查. 可能有一部分学生提前预习过,在我写登录SQL的时候,他们鄙视我说:“老师你这SQL有注入,随便都 ...

  5. nginx代理https站点(亲测)

    nginx代理https站点(亲测) 首先,我相信大家已经搞定了nginx正常代理http站点的方法,下面重点介绍代理https站点的配置方法,以及注意事项,因为目前大部分站点有转换https的需要所 ...

  6. 理解JavaScript中的“this”

    对于javascript的初学者来说,一般对“this”关键字都感到非常迷惑.本文的目的旨在让你全面的了解“this”,理解在每一个情景下如何使用“this”,希望通过本文,可以帮助同学们不在害怕“t ...

  7. Maven实战:pom.xml与settings.xml

    pom.xml与settings.xml pom.xml与setting.xml,可以说是Maven中最重要的两个配置文件,决定了Maven的核心功能,虽然之前的文章零零碎碎有提到过pom.xml和s ...

  8. 如何设计一门语言(七)——闭包、lambda和interface

    人们都很喜欢讨论闭包这个概念.其实这个概念对于写代码来讲一点用都没有,写代码只需要掌握好lambda表达式和class+interface的语义就行了.基本上只有在写编译器和虚拟机的时候才需要管什么是 ...

  9. Entity Framework 6 Recipes 2nd Edition(10-7)译 -> TPH继承模型中使用存储过程

    10-7. TPH继承模型中使用存储过程 问题 用一个存储过程来填充TPH继承模型的实体 解决方案 假设已有如Figure 10-7所示模型. 我们有两个派生实体: Instructor(教员)和St ...

  10. 安装keil MDK5

    昨天买的stm32板子今天就到了,顺丰速度确实很快. 我这是刚开始整STM32,首先需要下载keil MDK,直接杀去keil官网下载,其实对于学习者,我建议大家下载软件能去官网就去官网,尽量少用二道 ...