delete duplicate on multidimensional array and tak

2019-09-02 22:41发布

问题:

Suppose I have this array

[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
    )

What should I do in PHP to pick uniques based on revision_no vendor_no and document_number. And then for those having the same vendor_no and document_number take only the one having the highest revision_no.

So the result will be like:

[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:

$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()
);

This code does not preserve the keys from the original array. A solution that preserves the keys can be implemented in a similar fashion using array_walk().