-3

I want to get the IDs which are in first column of CSV file according to the values in second column having same data. I mean if code is EF in csv then its IDs value should get 1,2,3,4,5 and so on for other codes. SO i took all code values in string adn explode it in array a dn tried to get the ids with matching code values but not succeeded. Please help

CSV file:

1,EF,Total Amount Invested,1190000,600000,360000,120000,
2,EF,Market Value,2457939,852578,453191,132471,2019/12/31
3,EF,Returns (Annualised),14.01,14.03,15.54,19.8,
4,EF,Benchmark Returns (Annualised),11.48,11.51,11.38,14.05,
5,EF,Additional Benchmark Returns (Annualised),12.23,12.75,13.42,16.28,
6,AF,Total Amount Invested,910000,600000,360000,120000,
7,AF,Market Value,1636525,845919,434171,132677,2019/12/31
8,AF,Returns (Annualised),15.13,13.71,12.56,20.14,
9,AF,Benchmark Returns (Annualised),11.94,11.51,11.38,14.05,
10,AF,Additional Benchmark Returns (Annualised),12.31,10.57,9.2,12.48,

My Code:

$fileName_sip = '/var/www/html/php/csv/file.csv';

$file_sip = fopen($fileName_sip, "r");
fseek($file_sip, 1, SEEK_CUR);
$sc_str = "EF,CF,TA,TS,BD,EF,ST,IS,TP,AG,MC,DB,CM,IF,CO,AF,O,EA,ES,CG,GO,NE,DE,ML,WF,EH,US,SC,OG,MM,ON,NI,RA,RC,RD";
$sc_arr = explode(',', $sc_str);
//echo "<pre>";print_r($sc_arr);die;
$i=0;
$ids =array();
for ($i=0; $i < count($sc_arr); $i++) { 

    while (($col_sip[] = fgetcsv($file_sip, 10000, ",")) !== FALSE) {

        if ($col_sip[1] == $sc_arr[$i]) {
            $ids[$i] = $col_sip[0];
        }
    }
}
echo "<pre>";print_r($ids);

I want ouput to be:

EF - 1,2,3,4,5
AF   6,7,8,9,10
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Panky
  • 1
  • You have your while and for loops the wrong way round, although there are much easier ways to do this – RiggsFolly Feb 26 '20 at 11:51
  • 2
    Do you need to get an empty array for codes that are not present in your CSV to begin with? If not, you don’t need to provide an array with all the possible values upfront, you just loop over your data using _one_ loop, and put the ID into the correct entry of your target array. – CBroe Feb 26 '20 at 11:52
  • 1
    `while($col_sip[] = fgetcsv())` is wrong, that will _append_ a new item to `$col_sip` each time. This should just be `while($col_sip = fgetcsv())` (and the for loop completely removed, see comment above.) And inside the loop, you just do `$ids[$col_sip[1]][] = $col_sip[0];` – CBroe Feb 26 '20 at 11:55

1 Answers1

0

Here is a simplified version which uses in_array() to check for the correct code value and then just builds 2 sub arrays in the $ids array using the code as the key.

Then it is just a case of presentation of the data, so just loop over the array you have created and generate the output in whatever format you like.

$file_sip = fopen($fileName_sip, "r");
$sc_str = "EF,CF,TA,TS,BD,EF,ST,IS,TP,AG,MC,DB,CM,IF,CO,AF,O,EA,ES,CG,GO,NE,DE,ML,WF,EH,US,SC,OG,MM,ON,NI,RA,RC,RD";
$sc_arr = explode(',', $sc_str);
$ids = [];

while (($row = fgetcsv($file_sip, 10000, ",")) !== FALSE) {
    if (in_array($row[1], $sc_arr)) {
        $ids[$row[1]][] = $row[0];
    }
}
print_r($ids);

// make into a string
foreach ($ids as $key => $id){
    echo $key . ' - ' . implode(',', $id) . PHP_EOL;
}

The first loop creates the $ids array like this

Array
(
    [EF] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
        )

    [AF] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
            [3] => 9
            [4] => 10
        )

)

And the second outputs the results, but you can change that to suit your actual needs.

EF - 1,2,3,4,5
AF - 6,7,8,9,10
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149