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.

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

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3