PHP中模拟JSONArray
前面整理过一篇文章,描述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的更多相关文章
- 如何在C#中模拟C++的联合(Union)?[C#, C++] How To Simulate C++ Union In C#?
1 什么是联合? 联合(Union)是一种特殊的类,一个联合中的数据成员在内存中的存储是互相重叠的.每个数据成员都在相同的内存地址开始.分配给联合的存储区数量是“要包含它最大的数据成员”所需的内存数. ...
- Python中模拟enum枚举类型的5种方法分享
这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下 以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...
- 在C#中模拟Javascript的setTimeout方法
在C#中模拟Javascript的setTimeout方法 背景 每种语言都有自己的定时器(Timer),很多人熟悉Javascript中的setInterval和setTimeout,在Javasc ...
- iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。
转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...
- 在SoapUI中模拟用户操作
SoapUI作为一款接口测试工具,具有极大的灵活性和拓展性.它可以通过安装插件,拓展其功能.Selenium作为一款Web自动化测试插件可以很好的与SoapUI进行集成.如果要在SoapUI中模拟用户 ...
- 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/ ...
- php中模拟多继承如何实现
php中模拟多继承如何实现 一.总结 一句话总结:其实你继承别人也是想调用别人类里面的方法和属性,所以可以这样做:这本类中创建目标类的对象,然后通过这个对象来调用方法和属性,这样比继承来的方便. 二. ...
- .net中模拟键盘和鼠标操作
原文:.net中模拟键盘和鼠标操作 周银辉 其实SendKeys类提供的方法蛮好用的,可惜的是WPF中不能用了,说是WPF的消息循环方式改成了Dispatcher,所以直接调用System.Windo ...
- CSS中模拟父元素选择器
很多情况下,我们需要找到父元素,但可惜的是css中并没有这样的一个选择器. 至于原因可以看张鑫旭的如何在CSS中实现父选择器效果这篇文章. 简单来说这个实现并不是真正的父元素选择器,只是利用其它思路来 ...
随机推荐
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- 一行代码实现java list去重
1.不带类型写法: 1 List listWithoutDup = new ArrayList(new HashSet(listWithDup)); 2.带类型写法(以String类型为例):1)Ja ...
- Android 扫描条形码(Zxing插件)
使用Android Studio 一.在build.gradle(Module:app)添加代码 下载,调用插件 1 apply plugin: 'com.android.application' ...
- Linux环境变量设置
修改环境变量PATH 最近为root添加一个环境变量发现sudo su进去没有变化所以总结了一下所有设置环境变量的方法: 查看PATH:echo $PATH 直接在命令行修改,就可以使用,但是只有在当 ...
- Storm介绍(一)
作者:Jack47 PS:如果喜欢我写的文章,欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 内容简介 本文是Storm系列之一,介绍了Storm的起源,Storm ...
- 关于asp.net利用mono部署到Linux上的一些说明
linuxdot.net社区群:102732979(如果你认为你已经在.NET跨平台方面有足够的经验,请参加这个群:103810355). 其中有各种大神,嘿嘿,如果你有问题可以来咨询,完全无偿的免费 ...
- 我正在使用Xamarin的跨平台框架—Xamarin.Android回忆录
一.缘起 在自己给别家公司做兼职外包的时候,已经明确知道外包的活不是那么好干的,一般在经历了初期热血澎湃的激情后,逐渐冷淡,愤怒,再冷淡,再愤怒…,听上去好像高潮迭起,但令人尴尬的是,这高潮迭起我们都 ...
- 魅力 .NET:从 Mono、.NET Core 说起
前段时间,被问了这样一个问题:.NET 应用程序是怎么运行的? 当时大概愣了好久,好像也没说出个所以然,得到的回复是:这是 .NET 程序员最基本的...呵呵! 微软开源,其实不只是对 .NET 本身 ...
- .NET里简易实现IoC
.NET里简易实现IoC 前言 在前面的篇幅中对依赖倒置原则和IoC框架的使用只是做了个简单的介绍,并没有很详细的去演示,可能有的朋友还是区分不了依赖倒置.依赖注入.控制反转这几个名词,或许知道的也只 ...
- 用java开发微信公众号:接收和被动回复普通消息(三)
上篇说完了如何接入微信公众号,本文说一下微信公众号的最基本功能:普通消息的接收和回复.说到普通消息,那么什么是微信公众号所定义的普通消息呢,微信开发者文档中提到的接收的普通消息包括如下几类: 1.文本 ...