PHP 2次元配列、マルチパラメータの重量除去

2848 ワード

/**
**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
        )

)