PHP二维数组,多参数去重

/**
**PHP二维数组,多参数去重
**$arr 原始数组
**$filter 条件,多条件传数组
**return new去重后数组
*/
function array_unique_fb($arr=array(),$filter){   
    $res = array();      
    foreach ($arr as $key => $value) {
        $newkey='';
        if (is_array($filter)) {
            foreach ($filter as $fv) {
                $newkey.=$value[$fv];
            }
        }else{
            $newkey=$value[$filter];
        }
        foreach ($value as $vk => $va) {
            if (isset($res[$newkey])) {
                $res[$newkey][$vk]=$va;
            }else{
                $res[$newkey][$vk]=$va;
            }
        }
    }
    return $res;
}

实测

$waredata=array (
  21007 => 
  array (
    'wareid' => '2',
    'warename' => '南京仓库',
    'wareno' => 'nj0001',
    'forwarename' => '管家wms仓储',
    'forwareno' => '1001',
  ),
  216 => 
  array (
    'wareid' => '2',
    'warename' => '南京仓库',
    'wareno' => 'nj0001',
    'forwarename' => '胡工荷兰OMS仓库别动',
    'forwareno' => '10',
  ),
  112 => 
  array (
    'wareid' => '1',
    'warename' => '北京仓库',
    'wareno' => 'bj0001',
    'forwarename' => '胡工荷兰OMS仓库别动',
    'forwareno' => '10',
  ),
  211 => 
  array (
    'wareid' => '2',
    'warename' => '南京仓库',
    'wareno' => 'nj0001',
    'forwarename' => '胡工荷兰OMS仓库别动',
    'forwareno' => '10',
  ),
  111 => 
  array (
    'wareid' => '1',
    'warename' => '北京仓库',
    'wareno' => 'bj0001',
    'forwarename' => '胡工荷兰OMS仓库别动',
    'forwareno' => '10',
  ),
);

$tem=array_unique_fb($waredata,array('wareid','forwareno'));
echo "
";print_r($tem);die();

输出

Array
(
    [21001] => Array
        (
            [wareid] => 2
            [warename] => 南京仓库
            [wareno] => nj0001
            [forwarename] => 管家wms仓储
            [forwareno] => 1001
        )

    [210] => Array
        (
            [wareid] => 2
            [warename] => 南京仓库
            [wareno] => nj0001
            [forwarename] => 胡工荷兰OMS仓库别动
            [forwareno] => 10
        )

    [110] => Array
        (
            [wareid] => 1
            [warename] => 北京仓库
            [wareno] => bj0001
            [forwarename] => 胡工荷兰OMS仓库别动
            [forwareno] => 10
        )

)

你可能感兴趣的:(PHP二维数组,多参数去重)