0

I have seen few similar examples but it is still not working. csv data file "data1.csv" is as below:

symbol,num1,num2
QCOM,10,100
QCOM,20,200
QCOM,30,300
QCOM,40,400
CTSH,10,111
CTSH,20,222
CTSH,30,333
CTSH,40,444
AAPL,10,11
AAPL,20,22
AAPL,30,33
AAPL,40,44

--end of file ----

$inputsymbol = QCOM ; // $inputsymbol will come from html.works fine.

I want to read the csv file and fetch lines that matches symbol = QCOM. and convert it in to array $data1 to plot line chart for num1 and num2 as below.

$data1 = array ( 
array(10,100),
array(20,200),
array(30,300),
array(40,400)
);

Note: 1. no comma at the end of each csv lines in csv datafile. 2. Multiple symbols in same file. so the lines that match symbols only
should be included in $data1.

==============

Mark's soluition solves the problem. Now to make the data access faster (for a very large csv file), I have (externally) formatted same data as below. Question is how it can automatically extract headers and then for the data1 array?

symbol,1/1/2015,1/2/2015,1/3/2015,1/4/2015
QCOM,100,200,300,400
CTSH,11,22,33,44
AAPL,10,11,12,13

Note that the number of fields in header is not fixed. (it will increase every month). But the data will also increse accordingly.

1 Answers1

0

Not complicated:

$inputsymbol = 'QCOM';

$data1 = [];
$fh = fopen("data1.csv", "r"));
while (($data = fgetcsv($fh, 1024)) !== FALSE) {
    if ($data[0] == $inputsymbol) {
        unset($data[0]);
        $data1[] = $data;
    }
}
fclose($fh);

So where exactly are you having the problem?

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Then tell us what version of PHP you're running as you're obviously running an unsupported version; because PHP short array syntax was introduced in PHP 5.4. – Mark Baker Mar 25 '15 at 14:51
  • Replace `$data1 = [];` with `$data1 = array();` for versions of PHP that aren't supported any more – Mark Baker Mar 25 '15 at 14:51
  • Thanks Mark. I will check on php version. Actually after commenting $data1 = []; the code worked. – user2188923 Mar 25 '15 at 15:00
  • My second challange is...the actual csv file that i will be using has several thousand rows. just 3 columns. any comments what will be faster solution to search data in that situation. The cav data format is exactly same as in example. – user2188923 Mar 25 '15 at 15:02
  • If you know that all the entries matching your $inputsymbol will be grouped together, then you can break from the loop once you've found the first value that isn't a match (after retrieving all those that are a match). But what runtimes are you talking about to process the CSV file? – Mark Baker Mar 25 '15 at 15:13
  • symbol,day,value,day,value,day,value,day,value QCOM,10,100,20,200,30,300,31,400 CTSH,10,111,20,111,30,333,31,444 AAPL,10,11,20,22,30,33,31,44 – user2188923 Mar 25 '15 at 19:35
  • Mark, thanks for the suggestion. I tried that as: list($symbol,$day1,$value1.$day2,$value2,$day3,$value3,$day4,$value4) = fgetscv($fh) ; Is it possible to extract headers automatically from csv file and then use that in list()? – user2188923 Mar 25 '15 at 19:41
  • just ignore my list() coding attempt. For the exact same problem, how can i get exact same $data1 array when the csv file been changed to one as you suggested. (as updated in original question too.). Thanks. – user2188923 Mar 25 '15 at 19:48