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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集