douyou8047 2014-12-17 12:56
浏览 38
已采纳

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

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条回答 默认 最新

  • dongpa3109 2014-12-17 13:14
    关注
    $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().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路