博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP中模拟JSONArray
阅读量:4312 次
发布时间:2019-06-06

本文共 2976 字,大约阅读时间需要 9 分钟。

前面整理过一篇文章,描述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. 相关基础类

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 类可以依葫芦画瓢。

转载于:https://www.cnblogs.com/x3d/p/php-jsonarray.html

你可能感兴趣的文章
程序的基础知识
查看>>
在VIM中使用GDB调试 – 使用vimgdb
查看>>
python爬虫---从零开始(五)pyQuery库
查看>>
POJ2236(KB5-A)
查看>>
Centos MySQL数据库迁移详细步骤
查看>>
2初出茅庐--初级篇2.1
查看>>
新建 WinCE7.0 下的 Silverlight 工程
查看>>
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>