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".

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

you have forgotten a comma in your example $string = implode(",", $aff);
So I did - my keyboard does not like me today. Thank you!
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);

Comments

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,", ");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.