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

这篇文章主要介绍了PHP实现的多维数组去重操作,结合实例形式分析了php序列化json字符串及数组转换、去重等相关操作技巧,需要的朋友可以参考下。

本文实例讲述了PHP实现的多维数组去重操作,分享给大家供大家参考,具体如下:

最近用到了一个获取历史会议列表的问题,需要去这个多维数组去重后返回返回给客户端,这里使用了几个php的组装函数,分享下,示例代码如下:

  1. <?php
  2. $str = '{
  3. "status": 0,
  4. "result": [
  5. {
  6. "hostName": "杨新",
  7. "pcode2": "41675441",
  8. "email": "xin.yang@xx.cn",
  9. "selfName": "邵兵"
  10. },
  11. {
  12. "hostName": "杨新",
  13. "pcode2": "41675441",
  14. "email": "xin.yang@xx.cn",
  15. "selfName": "邵兵"
  16. },
  17. {
  18. "hostName": "中信国安科技控股有限公司",
  19. "pcode2": "9730284461",
  20. "email": "liuxiao@gakj.xx.com",
  21. "selfName": "邵兵"
  22. },
  23. {
  24. "hostName": "中信国安科技控股有限公司",
  25. "pcode2": "9730284461",
  26. "email": "liuxiao@gakj.xx.com",
  27. "selfName": "邵兵"
  28. },
  29. {
  30. "hostName": "刘惠",
  31. "pcode2": "64068956",
  32. "email": "huijie.liu@xx.cn",
  33. "selfName": "邵兵"
  34. },
  35. {
  36. "hostName": "刘惠",
  37. "pcode2": "64068956",
  38. "email": "huijie.liu@xx.cn",
  39. "selfName": "邵兵"
  40. },
  41. {
  42. "hostName": "刘凯南",
  43. "pcode2": "14818360",
  44. "email": "kainan.liu_3@xx.cn",
  45. "selfName": "邵兵"
  46. },
  47. {
  48. "hostName": "刘凯南",
  49. "pcode2": "14818360",
  50. "email": "kainan.liu_3@xx.cn",
  51. "selfName": "邵兵"
  52. },
  53. {
  54. "hostName": "宋纬奇",
  55. "pcode2": "62947142",
  56. "email": "weiqi.song@xx.cn",
  57. "selfName": "邵兵"
  58. },
  59. {
  60. "hostName": "宋纬奇",
  61. "pcode2": "62947142",
  62. "email": "weiqi.song@xx.cn",
  63. "selfName": "邵兵"
  64. }
  65. ],
  66. "content_type": "application/json"
  67. }';
  68. $res = json_decode($str,true);
  69. echo 'num: '.count($res['result']);
  70. print_r($res['result']);
  71. echo "</br></br></br>";
  72. $rrr = $res['result'];
  73. $serializeArrs = array_map('serialize',$rrr);
  74. $uniqueArrs = array_unique($serializeArrs);
  75. $unserializeArrs = array_map('unserialize',$uniqueArrs);
  76. print_r($unserializeArrs);
  77. echo "</br></br></br>";
  78. echo json_encode($unserializeArrs);
  79. exit;

运行结果:

  1. 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] => 邵兵 ) )
  2. 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] => 邵兵 ) )
  3. {"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()的结合使用。