0

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 Answers1

0

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.

vrijdenker
  • 1,371
  • 1
  • 12
  • 25
  • thank you much, one problem. It didn't output the first record: `LR8025BLKMD 58 4` –  Sep 24 '14 at 21:36
  • means it skips the first result. It prints only 3 when it should 4. –  Sep 24 '14 at 21:37
  • That's because I assummed the first record was the columnnames. You'll have to remove the lines stating "skip first line". (I'll edit my post) – vrijdenker Sep 24 '14 at 21:38
  • perfectly handled. you impress me man. thank you very much. i need the results that way. –  Sep 24 '14 at 21:42
  • wished to give you 10+ vote for this. :) how do you code so well? I want to a good programmer but I am failed to do so yet. –  Sep 24 '14 at 21:42
  • No problem mate. Just practise a lot. And also try to search on php.net a lot. While I am a well-trained programmer I am also lazy. The first part of this code was just copied from PHP.net: http://nl1.php.net/manual/en/function.fgetcsv.php – vrijdenker Sep 24 '14 at 21:45
  • Thank You very much for the tips brother. I will follow your tips for the future. –  Sep 24 '14 at 21:51
  • Can you please explain me the logic of your code for some good sake? thanks. –  Sep 24 '14 at 22:06
  • The comments in the code explain the logic and you can lookup each function I use on php.net – vrijdenker Sep 25 '14 at 06:26