本文实例讲述了PHP实现的多维数组去重操作。分享给大家供大家参考,具体如下:
最近用到了一个获取历史会议列表的问题,需要去这个多维数组去重后返回返回给客户端,这里使用了几个php的组装函数,分享下,示例代码如下:
<"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()
的结合使用。
关于序列化和json编码的效率对比的文章,可以参考 https://www.jb51.net/article/36177.htm
PS:本站还有两款比较简单实用的在线文本去重复工具,推荐给大家使用:
在线去除重复项工具:
http://tools.jb51.net/code/quchong
在线文本去重复工具:
http://tools.jb51.net/aideddesign/txt_quchong
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《PHP常用遍历算法与技巧总结》、《php字符串(string)用法总结》、《php常用函数与技巧总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》及《PHP数学运算技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 老头杯第二届规则是什么 英雄联盟老头杯第二届规则介绍
- 王崴-爵士听堂.蓝色波萨(HQCD)[WAV+CUE]
- 群星《欧美动听情歌·柔情第5季》2CD【DTS-WAV分轨】
- [极品珍藏]德意志进行曲集卡拉扬SACD[WAV+CUE]
- 前暴雪制作人呼吁反击DEI 玩家:夺回文化!
- 腾讯证实子公司Sharkmob大规模裁员:整个市场很低迷
- 荣耀加冕 问鼎冠军 中国代表队获第四届东亚电竞锦标赛团体总冠军
- 污甩乐队.2024-Let.the.dirt.left【摩登天空】【FLAC分轨】
- 杜德伟.1994-ALL.FOR.YOU(英)【滚石】【WAV+CUE】
- 群星.2013-百代好声音ADMSCD1【EMI百代】【WAV+CUE】
- 群星《私人音乐精选示范碟》PRIVATEMUSIC 发烧唱片名碟[WAV+CUE][1.1G]
- 山口百惠《あなたへの子守唄》日本索尼钢刻字首版[WAV分轨][1.1G]
- 群星《宝丽金20周年特别发烧版》1:1母盘直刻限量编号[低速原抓WAV+CUE][1G]
- 凤飞飞.1984-仲夏(夏艳)[WAV]
- 常安《民歌红·江南燕》DTS-ES6.1[WAV]