本文實(shí)例講述了PHP實(shí)現(xiàn)的多維數(shù)組去重操作。分享給大家供大家參考,具體如下:
最近用到了一個(gè)獲取歷史會(huì)議列表的問(wèn)題,需要去這個(gè)多維數(shù)組去重后返回返回給客戶端,這里使用了幾個(gè)php的組裝函數(shù),分享下,示例代碼如下:
?php
$str = '{
"status": 0,
"result": [
{
"hostName": "楊新",
"pcode2": "41675441",
"email": "xin.yang@xx.cn",
"selfName": "邵兵"
},
{
"hostName": "楊新",
"pcode2": "41675441",
"email": "xin.yang@xx.cn",
"selfName": "邵兵"
},
{
"hostName": "中信國(guó)安科技控股有限公司",
"pcode2": "9730284461",
"email": "liuxiao@gakj.xx.com",
"selfName": "邵兵"
},
{
"hostName": "中信國(guó)安科技控股有限公司",
"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;
運(yùn)行結(jié)果:
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] => 中信國(guó)安科技控股有限公司 [pcode2] => 9730284461 [email] => liuxiao@gakj.xx.com [selfName] => 邵兵 ) [3] => Array ( [hostName] => 中信國(guó)安科技控股有限公司 [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] => 中信國(guó)安科技控股有限公司 [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"}}
關(guān)于php的序列化的確是幾年前才使用的技術(shù),目前大家更多的是使用json_encode
,且后者效率也比php的序列化高。
序列化也還是很有用的。上面的例子中主要是函數(shù)array_map()
和array_unique()
的結(jié)合使用。
關(guān)于序列化和json編碼的效率對(duì)比的文章,可以參考 https://www.jb51.net/article/36177.htm
PS:本站還有兩款比較簡(jiǎn)單實(shí)用的在線文本去重復(fù)工具,推薦給大家使用:
在線去除重復(fù)項(xiàng)工具:
http://tools.jb51.net/code/quchong
在線文本去重復(fù)工具:
http://tools.jb51.net/aideddesign/txt_quchong
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php常用函數(shù)與技巧總結(jié)》、《PHP錯(cuò)誤與異常處理方法總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- PHP二維數(shù)組的去重問(wèn)題解析
- php二維數(shù)組合并及去重復(fù)的方法
- php冒泡排序、快速排序、快速查找、二維數(shù)組去重實(shí)例分享
- PHP二維數(shù)組去重算法
- php三維數(shù)組去重(示例代碼)
- PHP實(shí)現(xiàn)二維數(shù)組去重功能示例
- PHP二維數(shù)組去重實(shí)例分析
- PHP實(shí)現(xiàn)一維數(shù)組與二維數(shù)組去重功能示例
- php數(shù)組去重實(shí)例及分析
- PHP數(shù)組去重的更快實(shí)現(xiàn)方式分析
- php 數(shù)組元素快速去重