在多维数组中删除重复,并在PHP中具有最高价值 [英] delete duplicate on multidimensional array and take those having highest value in PHP

查看:206
本文介绍了在多维数组中删除重复,并在PHP中具有最高价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个数组

[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_no document_number 。然后对于那些具有相同的 vendor_no document_number 的人只能使用最高$ revision_no

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.

所以结果将如下:

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


推荐答案

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

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().

这篇关于在多维数组中删除重复,并在PHP中具有最高价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆