0

I import a CSV file with the following code (extract):

while(($line = fgetcsv($csvFile, 0, "\t")) !== FALSE){
//some mysql insert
$aff[] = $line[12];
}

Now I need to get some items to use them later in the code. I added the $aff variable, but was not able to create a string from the items. The final string should be seperated with a comma: a, b, c How can I do that? If I print the $aff out, it only says "Array".

Francis
  • 343
  • 2
  • 5
  • 16

3 Answers3

4

Use php's implode function -

http://php.net/manual/en/function.implode.php

$string = implode(",", $aff);

That will create a string separated by commas from your array.

Kwahn
  • 446
  • 2
  • 12
2

This is a very basic PHP question. Googling something like concatenate array or something should give you the answer right away. The correct approach would be to use the function implode with a separator:

echo implode(', ', $aff);

Also note that you should create the array outside of your loop if you don't already do this:

// Create empty array
$aff = [];

while(($line = fgetcsv($csvFile, 0, "\t")) !== FALSE){
//some mysql insert
$aff[] = $line[12];
}

// Output the array
echo implode(', ', $aff);
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
0

Try this, use concatenating assignment operato and do rtrim and you got your solution

$str = "";
while(($line = fgetcsv($csvFile, 0, "\t")) !== FALSE){
//some mysql insert
$str  .= $line[12].", ";
}
echo echo rtrim($str,", ");
JON
  • 965
  • 2
  • 10
  • 28