0

CSV data needs to be converted to key 2 dimensional array

------------ file = csvdata.csv --------
symbol,date1,date2,date3,date4
QCOM,10,11,12,13 
CTSH,20,21,22,23
AAPL,41,42,43,44 
------------ end of csv file ----

$inputsymbol = QCOM ; // $inputsymbol will come from html FORM.

Note:

  1. Multiple symbols in same file. 1 line per symbol (or match)
  2. number of headers are not fixed. will increase with time and so is data.

data that match $inputsymbol only should be included in $data1 array which needs to be as below.

$data1 = array ( 
array(date1,10),
array(date2,11),
array(date3,12),
array(date4,13)
);
  • 2
    Use the code that I showed you in your previous question as the basis for reading the headings and the relevant symbol record from the csv into a 2d array; then transpose that array – Mark Baker Mar 26 '15 at 20:33

1 Answers1

1

solved...

$filename1 = "sample.csv";
$fp1 = fopen("$filename1","r");
$header1 = fgetcsv($fp1);
$col1 = count($header1) - 1;

while (($data = fgetcsv($fp1)) !== FALSE)
        {
            if ($data[0] == $inputsymbol)
            {
                for ($rows = 0; $rows<$col1 ; $rows++)
                {
                    $data1[$rows][0] = $header1[$rows+1];
                    $data1[$rows][1] = $data[$rows+1];                      
                }

            }

        }