Example of multidimensional array de duplication operation implemented by PHP

  • 2021-10-24 19:14:42
  • OfStack

In this paper, an example is given to describe the multi-dimensional array de-duplication operation implemented by PHP. Share it for your reference, as follows:

Recently, I used a problem of getting a list of historical meetings. I need to go to this multidimensional array and return it to the client. I use several php assembly functions here. Share the following example code:


<?php
$str = '{
  "status": 0,
  "result": [
    {
      "hostName": " Yang Xin ",
      "pcode2": "41675441",
      "email": "xin.yang@xx.cn",
      "selfName": " Shao Bing "
    },
    {
      "hostName": " Yang Xin ",
      "pcode2": "41675441",
      "email": "xin.yang@xx.cn",
      "selfName": " Shao Bing "
    },
    {
      "hostName": " CITIC Guoan technology holding co., ltd ",
      "pcode2": "9730284461",
      "email": "liuxiao@gakj.xx.com",
      "selfName": " Shao Bing "
    },
    {
      "hostName": " CITIC Guoan technology holding co., ltd ",
      "pcode2": "9730284461",
      "email": "liuxiao@gakj.xx.com",
      "selfName": " Shao Bing "
    },
    {
      "hostName": " Liu Hui ",
      "pcode2": "64068956",
      "email": "huijie.liu@xx.cn",
      "selfName": " Shao Bing "
    },
    {
      "hostName": " Liu Hui ",
      "pcode2": "64068956",
      "email": "huijie.liu@xx.cn",
      "selfName": " Shao Bing "
    },
    {
      "hostName": " Liu Kennan ",
      "pcode2": "14818360",
      "email": "kainan.liu_3@xx.cn",
      "selfName": " Shao Bing "
    },
    {
      "hostName": " Liu Kennan ",
      "pcode2": "14818360",
      "email": "kainan.liu_3@xx.cn",
      "selfName": " Shao Bing "
    },
    {
      "hostName": " Song Weiqi ",
      "pcode2": "62947142",
      "email": "weiqi.song@xx.cn",
      "selfName": " Shao Bing "
    },
    {
      "hostName": " Song Weiqi ",
      "pcode2": "62947142",
      "email": "weiqi.song@xx.cn",
      "selfName": " Shao Bing "
    }
  ],
  "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;

Run results:

num: 10Array ( [0] = > Array ( [hostName] = > Yang Xin [pcode2] = > 41675441 [email] = > xin.yang@xx.cn [selfName] = > Shao Bing) [1] = > Array ( [hostName] = > Yang Xin [pcode2] = > 41675441 [email] = > xin.yang@xx.cn [selfName] = > Shao Bing) [2] = > Array ( [hostName] = > CITIC Guoan Technology Holdings Co., Ltd. [pcode2] = > 9730284461 [email] = > liuxiao@gakj.xx.com [selfName] = > Shao Bing) [3] = > Array ( [hostName] = > CITIC Guoan Technology Holdings Co., Ltd. [pcode2] = > 9730284461 [email] = > liuxiao@gakj.xx.com [selfName] = > Shao Bing) [4] = > Array ( [hostName] = > Liu Hui [pcode2] = > 64068956 [email] = > huijie.liu@xx.cn [selfName] = > Shao Bing) [5] = > Array ( [hostName] = > Liu Hui [pcode2] = > 64068956 [email] = > huijie.liu@xx.cn [selfName] = > Shao Bing) [6] = > Array ( [hostName] = > Liu Kainan [pcode2] = > 14818360 [email] = > kainan.liu_3@xx.cn [selfName] = > Shao Bing) [7] = > Array ( [hostName] = > Liu Kainan [pcode2] = > 14818360 [email] = > kainan.liu_3@xx.cn [selfName] = > Shao Bing) [8] = > Array ( [hostName] = > Song Weiqi [pcode2] = > 62947142 [email] = > weiqi.song@xx.cn [selfName] = > Shao Bing) [9] = > Array ( [hostName] = > Song Weiqi [pcode2] = > 62947142 [email] = > weiqi.song@xx.cn [selfName] = > Shao Bing))


Array ( [0] = > Array ( [hostName] = > Yang Xin [pcode2] = > 41675441 [email] = > xin.yang@xx.cn [selfName] = > Shao Bing) [2] = > Array ( [hostName] = > CITIC Guoan Technology Holdings Co., Ltd. [pcode2] = > 9730284461 [email] = > liuxiao@gakj.xx.com [selfName] = > Shao Bing) [4] = > Array ( [hostName] = > Liu Hui [pcode2] = > 64068956 [email] = > huijie.liu@xx.cn [selfName] = > Shao Bing) [6] = > Array ( [hostName] = > Liu Kainan [pcode2] = > 14818360 [email] = > kainan.liu_3@xx.cn [selfName] = > Shao Bing) [8] = > Array ( [hostName] = > Song Weiqi [pcode2] = > 62947142 [email] = > weiqi.song@xx.cn [selfName] = > Shao Bing))


{"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"}}

Serialization of php is indeed a technology used only a few years ago. At present, people use more json_encode And the latter is more efficient than the serialization of php.

Serialization is also very useful. The above example is mainly a function array_map() And array_unique() The combination of.

For an article on the efficiency comparison between serialization and json encoding, please refer to https://www. ofstack. com/article/36177. htm

PS: This site also has two relatively simple and practical online text to repeat tools, recommended for everyone to use:

Online Duplicate Removal Tool:
http://tools.ofstack.com/code/quchong

Online text de-duplication tool:
http://tools.ofstack.com/aideddesign/txt_quchong

For more readers interested in PHP related contents, please check the special topics of this site: "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of PHP Common Traversal Algorithms and Skills", "Summary of php String (string) Usage", "Summary of php Common Functions and Skills", "Summary of PHP Error and Exception Handling Methods", "Introduction to PHP Basic Grammar", "Introduction to php Object-Oriented Programming" and "Summary of PHP Mathematical Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: