-
PHP实现的多维数组去重操作示例
这篇文章主要介绍了PHP实现的多维数组去重操作,结合实例形式分析了php序列化json字符串及数组转换、去重等相关操作技巧,需要的朋友可以参考下。
本文实例讲述了PHP实现的多维数组去重操作,分享给大家供大家参考,具体如下:
最近用到了一个获取历史会议列表的问题,需要去这个多维数组去重后返回返回给客户端,这里使用了几个php的组装函数,分享下,示例代码如下:
- <?php
- $str = '{
- "status": 0,
- "result": [
- {
- "hostName": "杨新",
- "pcode2": "41675441",
- "email": "xin.yang@xx.cn",
- "selfName": "邵兵"
- },
- {
- "hostName": "杨新",
- "pcode2": "41675441",
- "email": "xin.yang@xx.cn",
- "selfName": "邵兵"
- },
- {
- "hostName": "中信国安科技控股有限公司",
- "pcode2": "9730284461",
- "email": "liuxiao@gakj.xx.com",
- "selfName": "邵兵"
- },
- {
- "hostName": "中信国安科技控股有限公司",
- "pcode2": "9730284461",
- "email": "liuxiao@gakj.xx.com",
- "selfName": "邵兵"
- },
- {
- "hostName": "刘惠",
- "pcode2": "64068956",
- "email": "huijie.liu@xx.cn",
- "selfName": "邵兵"
- },
- {
- "hostName": "刘惠",
- "pcode2": "64068956",
- "email": "huijie.liu@xx.cn",
- "selfName": "邵兵"
- },
- {
- "hostName": "刘凯南",
- "pcode2": "14818360",
- "email": "kainan.liu_3@xx.cn",
- "selfName": "邵兵"
- },
- {
- "hostName": "刘凯南",
- "pcode2": "14818360",
- "email": "kainan.liu_3@xx.cn",
- "selfName": "邵兵"
- },
- {
- "hostName": "宋纬奇",
- "pcode2": "62947142",
- "email": "weiqi.song@xx.cn",
- "selfName": "邵兵"
- },
- {
- "hostName": "宋纬奇",
- "pcode2": "62947142",
- "email": "weiqi.song@xx.cn",
- "selfName": "邵兵"
- }
- ],
- "content_type": "application/json"
- }';
- $res = json_decode($str,true);
- echo 'num: '.count($res['result']);
- print_r($res['result']);
- echo "</br></br></br>";
- $rrr = $res['result'];
- $serializeArrs = array_map('serialize',$rrr);
- $uniqueArrs = array_unique($serializeArrs);
- $unserializeArrs = array_map('unserialize',$uniqueArrs);
- print_r($unserializeArrs);
- echo "</br></br></br>";
- echo json_encode($unserializeArrs);
- exit;
运行结果:
- num: 10Array ( [0] => Array ( [hostName] => 杨新 [pcode2] => 41675441 [email] => xin.yang@xx.cn [selfName] => 邵兵 ) [1] => Array ( [hostName] => 杨新 [pcode2] => 41675441 [email] => xin.yang@xx.cn [selfName] => 邵兵 ) [2] => Array ( [hostName] => 中信国安科技控股有限公司 [pcode2] => 9730284461 [email] => liuxiao@gakj.xx.com [selfName] => 邵兵 ) [3] => Array ( [hostName] => 中信国安科技控股有限公司 [pcode2] => 9730284461 [email] => liuxiao@gakj.xx.com [selfName] => 邵兵 ) [4] => Array ( [hostName] => 刘惠 [pcode2] => 64068956 [email] => huijie.liu@xx.cn [selfName] => 邵兵 ) [5] => Array ( [hostName] => 刘惠 [pcode2] => 64068956 [email] => huijie.liu@xx.cn [selfName] => 邵兵 ) [6] => Array ( [hostName] => 刘凯南 [pcode2] => 14818360 [email] => kainan.liu_3@xx.cn [selfName] => 邵兵 ) [7] => Array ( [hostName] => 刘凯南 [pcode2] => 14818360 [email] => kainan.liu_3@xx.cn [selfName] => 邵兵 ) [8] => Array ( [hostName] => 宋纬奇 [pcode2] => 62947142 [email] => weiqi.song@xx.cn [selfName] => 邵兵 ) [9] => Array ( [hostName] => 宋纬奇 [pcode2] => 62947142 [email] => weiqi.song@xx.cn [selfName] => 邵兵 ) )
- Array ( [0] => Array ( [hostName] => 杨新 [pcode2] => 41675441 [email] => xin.yang@xx.cn [selfName] => 邵兵 ) [2] => Array ( [hostName] => 中信国安科技控股有限公司 [pcode2] => 9730284461 [email] => liuxiao@gakj.xx.com [selfName] => 邵兵 ) [4] => Array ( [hostName] => 刘惠 [pcode2] => 64068956 [email] => huijie.liu@xx.cn [selfName] => 邵兵 ) [6] => Array ( [hostName] => 刘凯南 [pcode2] => 14818360 [email] => kainan.liu_3@xx.cn [selfName] => 邵兵 ) [8] => Array ( [hostName] => 宋纬奇 [pcode2] => 62947142 [email] => weiqi.song@xx.cn [selfName] => 邵兵 ) )
- {"0":{"hostName":"\u6768\u65b0","pcode2":"41675441","email":"xin.yang@xx.cn","selfName":"\u90b5\u5175"},"2":{"hostName":"\u4e2d\u4fe1\u56fd\u5b89\u79d1\u6280\u63a7\u80a1\u6709\u9650\u516c\u53f8","pcode2":"9730284461","email":"liuxiao@gakj.xx.com","selfName":"\u90b5\u5175"},"4":{"hostName":"\u5218\u60e0","pcode2":"64068956","email":"huijie.liu@xx.cn","selfName":"\u90b5\u5175"},"6":{"hostName":"\u5218\u51ef\u5357","pcode2":"14818360","email":"kainan.liu_3@xx.cn","selfName":"\u90b5\u5175"},"8":{"hostName":"\u5b8b\u7eac\u5947","pcode2":"62947142","email":"weiqi.song@xx.cn","selfName":"\u90b5\u5175"}}
关于php的序列化的确是几年前才使用的技术,目前大家更多的是使用json_encode,且后者效率也比php的序列化高。
序列化也还是很有用的,上面的例子中主要是函数array_map()和array_unique()的结合使用。
出处:http://www.phpfensi.com/php/20211019/18191.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式