Srikanth AD
Srikanth AD

Reputation: 1854

Reading from CSV and merging content in rows into an array

pastebin link: http://pastebin.com/4YCdbrsP

Input CSV format,

number | itemid
1001, 121212
1001, 12991
1042, 99878
1042, 89776
1042, 87777
1045, 11010
1046, 22299
1049, 9875

Expected Output

[1001 => {121212,12991}]
[1042 => {99878, 89776, 87777}]
[1045 => {11010}]
[1046 => {22299}]
[1049 => {9875}]

I am trying to read each line from a CSV file which has contents as specified above (two columns, each row containing "number" as the first column and an associated "itemid" as the second column.

If the next line has the same "number" then, I would like its corresponding "itemid" to be pushed to an array. Repeat this, if the next line(s) have the same "number".

Following is where I have gotten so far.

  $row = 0;

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE && $row < 8) {
        $sku = $data[0];
        $upc = $data[1];
        $skuUpcList = array($sku => array());
        array_push($skuUpcList[$sku], $upc);

        $row++;

        while(($data = fgetcsv($handle, 1000, ",")) !== FALSE && $row < 8) {
            if($data[0] == $sku) {
                array_push($skuUpcList[$sku], $data[1]);
                $row++;
            } else {
                $sku = $data[0];
                $upc = $data[1];
                $skuUpcList = array($sku => array());
                array_push($skuUpcList[$sku], $data[1]);
                $row++; 
            }
            $skuUpcList = array_unique($skuUpcList);
            var_dump($skuUpcList); 
            echo  '<br>';
        }


    }

=== Output of the above script ===

array(1) { [1001]=> array(2) { [0]=> string(6) "121212" [1]=> string(5) "12991" } } 
array(1) { [1042]=> array(1) { [0]=> string(5) "99878" } } 
array(1) { [1042]=> array(2) { [0]=> string(5) "99878" [1]=> string(5) "89776" } } 
array(1) { [1042]=> array(3) { [0]=> string(5) "99878" [1]=> string(5) "89776" [2]=> string(5) "87777" } } 
array(1) { [1045]=> array(1) { [0]=> string(5) "11010" } } 
array(1) { [1046]=> array(1) { [0]=> string(5) "22299" } } 
array(1) { [1049]=> array(1) { [0]=> string(4) "9875" } } 

As you can see, even though the script runs fine, I am trying to make sure that the script echos each "number" only once but, as per the above output the "number" 1042 appears three times.

I have tried to move the var_dump() statement outside the 2nd while loop but, it doesn't seem to help.

Any help regarding this would be helpful.

pastebin link: http://pastebin.com/4YCdbrsP

Upvotes: 0

Views: 987

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94662

This seems to work quite nicely:

<?php

$handle = fopen("xxx.csv", "r");

$row = 0;
$skuUpcList = array();

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE && $row < 8) {
    $sku = $data[0];
    $upc = $data[1];

    if ( array_key_exists($sku, $skuUpcList) ) {
        $skuUpcList[$sku][] = $upc;
    } else {
        $skuUpcList[$sku] = array($upc);
    }
    $row++;
}

print_r( $skuUpcList );

Upvotes: 1

Naktibalda
Naktibalda

Reputation: 14110

$skuUpcList = array();

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $skuUpcList[$data[0]] []= $data[1];
}

Upvotes: 3

Related Questions