PHP多维数组去重

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




$str = '{
    "status": 0,
    "result": [
        {
            "hostName": "杨新",
            "pcode2": "41675441",
            "email": "[email protected]",
            "selfName": "邵兵"
        },
        {
            "hostName": "杨新",
            "pcode2": "41675441",
            "email": "[email protected]",
            "selfName": "邵兵"
        },
        {
            "hostName": "中信国安科技控股有限公司",
            "pcode2": "9730284461",
            "email": "[email protected]",
            "selfName": "邵兵"
        },
        {
            "hostName": "中信国安科技控股有限公司",
            "pcode2": "9730284461",
            "email": "[email protected]",
            "selfName": "邵兵"
        },
        {
            "hostName": "刘惠",
            "pcode2": "64068956",
            "email": "[email protected]",
            "selfName": "邵兵"
        },
        {
            "hostName": "刘惠",
            "pcode2": "64068956",
            "email": "[email protected]",
            "selfName": "邵兵"
        },
        {
            "hostName": "刘凯南",
            "pcode2": "14818360",
            "email": "[email protected]",
            "selfName": "邵兵"
        },
        {
            "hostName": "刘凯南",
            "pcode2": "14818360",
            "email": "[email protected]",
            "selfName": "邵兵"
        },
        {
            "hostName": "宋纬奇",
            "pcode2": "62947142",
            "email": "[email protected]",
            "selfName": "邵兵"
        },
        {
            "hostName": "宋纬奇",
            "pcode2": "62947142",
            "email": "[email protected]",
            "selfName": "邵兵"
        }
    ],
    "content_type": "application/json"
}';


$res = json_decode($str,true);

echo  'num: '.count($res['result']);

print_r($res['result']);
echo "


";
$rrr = $res['result'];

$serializeArrs = array_map('serialize',$rrr);

$uniqueArrs = array_unique($serializeArrs);

$unserializeArrs = array_map('unserialize',$uniqueArrs);

print_r($unserializeArrs);
echo "


";
echo json_encode($unserializeArrs);

exit;


关于php的序列化的确是几年前才使用的技术,目前大家更多的是使用json_encode,且后者效率也比php的序列化高。

序列化也还是很有用的。上面的例子中主要是函数array_map()和array_unique()的结合使用。



关于序列化和json编码的效率对比的文章,可以参考 http://blog.csdn.net/hguisu/article/details/7651730


你可能感兴趣的:(PHP+MySQL技术)