duanji8615 2014-09-24 20:48
浏览 17
已采纳

如何根据唯一列获取CSV值(如果发现唯一列循环其某些列)

I have a CSV file who has row by row data. The scenerio is that 2 of my columns have same values all the time.

Suppose, if in the row 1, column 0 and column 1 have the same value it simple store it in array first with its other values whom I want to loop.

On the next iteration, it checks if column 0 and column 1 values already exists in previous array, so loop 1st column and that column data to new array till from the preceding rows column 0 and column 1 have different values.

Here is the CSV file data:

PickTicketNumber    OrderNumber LineItemUpc LineItemUnitPrice   LineItemQuantity                                                                                                                                                                                                    
218837  204127  LR8025BLKMD 58  4                                                                                                                                                                                                   
218837  204127  LR8025BLKLG 58  3                                                                                                                                                                                                   
218837  204127  LR8475HEASM 54  1                                                                                                                                                                                                   
218837  204127  LR8770ROYLG 53  1                                                                                                                                                                                                   
218838  204128  LR8475HEAXS 54  1                                                                                                                                                                                                   
218838  204128  LR8475HEASM 54  2                                                                                                                                                                                                   
218838  204128  LR8475HEAMD 54  2                                                                                                                                                                                                   
218838  204128  LR8475HEALG 54  1                                                                                                                                                                                                   
218838  204128  LR8917BLKXS 58  1                                                                                                                                                                                                   
218838  204128  LR8917BLKSM 58  1                                                                                                                                                                                                   
218838  204128  LR8917BLKMD 58  1                                                                                                                                                                                                   
218838  204128  LR8917BLKLG 58  1                                                                                                                                                                                                   
218838  204128  LR6382BLKXS 48  1                                                                                                                                                                                                   
218838  204128  LR6382BLKSM 48  2                                                                                                                                                                                                   
218838  204128  LR6382BLKMD 48  2                                                                                                                                                                                                   
218838  204128  LR6382BLKLG 48  1                                                                                                                                                                                                   
218838  204128  LR8475BLKSM 54  2                                                                                                                                                                                                   
218838  204128  LR8475BLKMD 54  2                                                                                                                                                                                                   
218838  204128  LR8475BLKLG 54  1                                                                                                                                                                                                   
218839  204130  LR8878HEAXL 63  1

As its clear here, 218837 204127 exists 4 times so i simply want it one time and loop its LineItemUpc LineItemUnitPrice LineItemQuantity.

In the final result I would like to see the result this way:

218837  204127 

one time only and if again it exists in next row so loop its LineItemUpc LineItemUnitPrice LineItemQuantity again. Means in the loop I want :

LR8025BLKMD 58  4                                                                                                                                                                                                   
LR8025BLKLG 58  3                                                                                                                                                                                                   
LR8475HEASM 54  1                                                                                                                                                                                                   
LR8770ROYLG 53  1

Same for rest of the elements as well.

Means one PickTicketNumber OrderNumber and loop all their values in the upcoming rows having same PickTicketNumber OrderNumber until and unless new PickTicketNumber OrderNumber comes up with new PickTicketNumber OrderNumber.

In the end my final result would be:

<reference>218837 / 204127</reference>
            <line>LR8025BLKMD</line>
                <price>58<price>
                <qty>3<qty>
            <line>LR8025BLKLG</line>
                <price>58<price>
                <qty>1<qty>
            <line>LR8475HEASM</line>
                <price>54<price>
                <qty>1<qty>
            <line>LR8770ROYLG</line>
            <price>53<price>
            <qty>1<qty>
  • 写回答

1条回答 默认 最新

  • doutang7707 2014-09-24 21:31
    关注

    I'm still not sure what you want as your final result, but I hope this gets you going:

    <?php
    
    $result = array();
    
    if (($handle = fopen("test.csv", "r")) !== FALSE) {
        $row = 0;
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            // Create a unique key based on the first two fields
            $unique = $data[0] . '-' . $data[1];
    
            // Test if this key already exists in the array. If not, create it.
            if (!array_key_exists($unique, $result)) {
                $result[$unique] = array(
                    'PickTicketNumber' => $data[0],
                    'OrderNumber' => $data[1],
                    'Data' => array(),
                );
            }
    
            // Append the actual data (all columns except the first two)
            $result[$unique]['Data'][] = array_slice($data, 2);
        }
        fclose($handle);
    }
    
    echo "<pre>".print_r($result,1)."</pre>";
    

    This should give something like:

    Array(
        '218837-204127' => Array(
            'PickTicketNumber' => 218837,
            'OrderNumber' => 204127,
            'Data' => Array(
                array('LR8...MD',58,4),
                array('LR80..LG',58,3),
                etc.
            )
        ),
        '218838-204128' => Array(
            etc.
        ),
    )
    

    I hope that answers your question.

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

报告相同问题?

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据