PHP数组按两个字段值排序和删除重复项

时间:2014-12-23 07:48:26

标签: php arrays

我有一个像这样的数组结构

  [0]=>array(3) {
    ["Number"]=> "L1"
    ["Location"]=> "Location-A"
    ["Qty"]=>"1"
  }
  [1]=>array(3) {
    ["Number"]=> "L1"
    ["Location"]=> "Location-B"
    ["Qty"]=> "5"
  }
  [2]=> array(3) {
    ["Number"]=> "L1"
    ["Location"]=> "Location-B"
    ["Qty"]=> "4"
  }
  [3]=>array(3) {
    ["Number"]=> "L2"
    ["Location"]=>  "Location-B"
    ["Qty"]=>  "5"
  }

但我需要以下结构作为输出

 [0]=>array(3) {
    ["Number"]=> "L1"
    ["Location"]=> "Location-A"
    ["Qty"]=>"1"
  }
  [1]=> array(3) {
    ["Number"]=> "L1"
    ["Location"]=> "Location-B"
    ["Qty"]=> "4"
  }
  [2]=>array(3) {
    ["Number"]=> "L2"
    ["Location"]=>  "Location-B"
    ["Qty"]=>  "5"
  }

如何按号码和位置删除重复值?

ksort只适用于一个值,我需要删除两个值,我该如何实现这个PHP?

$ordered = array();
foreach ($data as $da) 
{           
    $ordered[$da['Number']] = $da;
    $ordered[$da['Location']] = $da;            
}
ksort($ordered);

3 个答案:

答案 0 :(得分:1)

创建新数组时连接两个字段:

foreach ($data as $da) {
    $result[$da['Number'] . '.' . $da['Location']] = $da;
}
$result = array_values($result); // Turn it back into indexed array

答案 1 :(得分:1)

试试这个..

 <?php
    $array = array(
        0 => array('Number'=>'L1','Location'=>'Location-A','Qty'=>'1'),
        1 => array('Number'=>'L1','Location'=>'Location-B','Qty'=>'5'),
        2 => array('Number'=>'L1','Location'=>'Location-B','Qty'=>'4'),
        3 => array('Number'=>'L2','Location'=>'Location-B','Qty'=>'5'),
    );
    $output =   array_values(array_intersect_key($array,array_unique(array_map(function($arrayval) {
        return $arrayval['Number'] . '.' .$arrayval['Location'];
    }, $array))
));
    print_r($output);

<强>输出

Array ( [0] => Array ( [Number] => L1 [Location] => Location-A [Qty] => 1 )
        [1] => Array ( [Number] => L1 [Location] => Location-B [Qty] => 5 ) 
        [2] => Array ( [Number] => L2 [Location] => Location-B [Qty] => 5 ) )

答案 2 :(得分:0)

试试这个:

function array_unique_c($array, Closure $comparer) {
    $result = array();
    for($i = 0; $i < count($array); $i++) {
        $duplicates = false;
        for($n = $i + 1; $n < count($array); $n++) {
            if ($comparer($array[$i], $array[$n])) {
                $duplicates = true;
                break;
            }
        }
        if(!$duplicates) {
            $result[] = $array[$i];
        }
    }

    return $result;
}

用法:

$uniqueArray = array_unique_c($a, function ($itemA, $itemB) {
    return $itemA['Number'] == $itemB['Number'] && $itemA['Location'] == $itemB['Location'];
});

输出:

array(3) {
    [0] => array(3) {
        ["Number"] => string(2) "L1"
        ["Location"] => string(10) "Location-A"
        ["Qty"] => string(1) "1"
    }
    [1] => array(3) {
        ["Number"]=> string(2) "L1"
        ["Location"]=> string(10) "Location-B"
        ["Qty"]=> string(1) "4"
    }
    [2]=> array(3) {
        ["Number"]=> string(2) "L2"
        ["Location"]=> string(10) "Location-B"
        ["Qty"]=> string(1) "5"
    }
}