0

I'm trying to collect data from a csv file and putting into a multidimensional array, to be later manipulated and put into a new csv.

Info on the CSVs (if needed)

The CSV contains orders from customers,and each line contains the quantity and SKU of one type of item in a given customer's order, as well as customer info such as address.

The reason why I'm using 3 dimensions for the array I collect the csv data in, is to separate lines into single customer orders.

Code

function order_data_collection() {

global $myfile;

$loopCount = 0;
$orderCount = 0;
$lineCount = 0;
$csvData = array();

$lastNum = "";



// actual data collection loop


    while(! feof($myfile)) {

        $loopCount++;

        $line = array();
        $line = fgetcsv($myfile);
        if (!$line=fgetcsv($myfile,4096,',','"')) continue;


        // cell data vars

        $currNum = $line[0];
        $currCountry = $line[6];
        $currQty = $line[8];
        $currSKU = $line[9];



        $isFirstOrder = true;



        if ($lastNum == $currNum) {
            $isFirstOrder = false; $lineCount++;
        } else {
            $isFirstOrder = true; $orderCount++;
            $lineCount = 0;
        }


        $parseCount = 0;
        foreach ($line as $x) {
            $csvData[$orderCount][$lineCount][$parseCount] = $x; $parseCount++;
        }                                       

        $lastNum = $currNum;

    }
}   
User0123456789
  • 760
  • 2
  • 10
  • 25
Ty5517
  • 1
  • Make sure `$csvData` has indexes for `$orderCount` and `$lineCount` before you add to it. You can't initialize them on the fly like this. – aynber Jan 04 '17 at 18:52
  • I did try to declare those indexes, in the foreach loop, but stil got the error – Ty5517 Jan 04 '17 at 19:44

0 Answers0