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

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

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动