删除多维数组上的副本并获取PHP中具有最高值的副本

时间:2014-12-17 12:56:27

标签: php multidimensional-array duplicate-removal

假设我有这个数组

[0] => Array
    (
        [revision_no] => 0
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 311560353071000
        [document_number] => 010.000-12.00000001
        [gross] => 34650000
        [vat] => 3465000
    )

[1] => Array
    (
        [revision_no] => 1
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 311560353071000
        [document_number] => 010.000-12.00000001
        [gross] => 44650000
        [vat] => 4465000
    )

[2] => Array
    (
        [revision_no] => 2
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 311560353071000
        [document_number] => 010.000-12.00000001
        [gross] => 34650000
        [vat] => 3465000
    )
[3] => Array
    (
        [revision_no] => 0
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 311560353071000
        [document_number] => 010.000-12.00000002
        [gross] => 34650000
        [vat] => 3465000
    )

[4] => Array
    (
        [revision_no] => 1
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 311560353071000
        [document_number] => 010.000-12.00000002
        [gross] => 34650000
        [vat] => 3465000
    )

[5] => Array
    (
        [revision_no] => 0
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 311560353071000
        [document_number] => 010.000-12.00000003
        [gross] => 34650000
        [vat] => 3465000
    )
[6] => Array
    (
        [revision_no] => 0
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 2132134923102931
        [document_number] => 010.000-12.00000003
        [gross] => 34650000
        [vat] => 3465000
    )

我应该如何在PHP中根据revision_no vendor_nodocument_number选择唯一身份用户。然后对于那些具有相同vendor_nodocument_number的人,只选择具有最高revision_no的人。

结果如下:

[2] => Array
    (
        [revision_no] => 2
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 311560353071000
        [document_number] => 010.000-12.00000001
        [gross] => 34650000
        [vat] => 3465000
    )
[3] => Array
    (
        [revision_no] => 1
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 311560353071000
        [document_number] => 010.000-12.00000002
        [gross] => 34650000
        [vat] => 3465000
    )

[5] => Array
    (
        [revision_no] => 0
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 311560353071000
        [document_number] => 010.000-12.00000003
        [gross] => 34650000
        [vat] => 3465000
    )
[6] => Array
    (
        [revision_no] => 0
        [invoice_type] => PK
        [transaction_id] => 5
        [vendor_number] => 2132134923102931
        [document_number] => 010.000-12.00000003
        [gross] => 34650000
        [vat] => 3465000
    )

1 个答案:

答案 0 :(得分:1)

$output = array_reduce(
    $input,
    function (array $carry, array $item) {
        // generate the key to identify the duplicates
        // Add $item['gross'] if needed
        $key = $item['vendor_number'].'/'.$item['document_number'];

        // If this is the first appearance of the key
        // then add the value to the partial list and return it
        if (! isset($carry[$key])) {
            $carry[$key] = $item;
            return $carry;
        }

        // A previous revision exists    
        // Check values in $item against those already existing in the list
        $old = $carry[$key];
        if ($old['revision_no'] < $item['revision_no']) {
            // This is a new revision, replace the old one
            $carry[$key] = $item;
        }

        // Return $carry (updated or not)
        return $carry;
    },
    array()
);

此代码不保留原始数组中的键。可以使用array_walk()以类似的方式实现保留密钥的解决方案。