欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP实现的多维数组去重操作示例

程序员文章站 2022-07-06 10:52:25
本文实例讲述了php实现的多维数组去重操作。分享给大家供大家参考,具体如下: 最近用到了一个获取历史会议列表的问题,需要去这个多维数组去重后返回返回给客户端,这里使用了几...

本文实例讲述了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()的结合使用。

关于序列化和json编码的效率对比的文章,可以参考

ps:本站还有两款比较简单实用的在线文本去重复工具,推荐给大家使用:

在线去除重复项工具:

在线文本去重复工具:

更多关于php相关内容感兴趣的读者可查看本站专题:《php数组(array)操作技巧大全》、《php常用遍历算法与技巧总结》、《php字符串(string)用法总结》、《php常用函数与技巧总结》、《php错误与异常处理方法总结》、《php基本语法入门教程》、《php面向对象程序设计入门教程》及《php数学运算技巧总结

希望本文所述对大家php程序设计有所帮助。