-3

I'm trying to use 'for loop' and write directly to csv without inserting into database and export as csv file.

<?php 
 require_once('function.php');

 for ($i=0; $i < 6000; $i++) { 
    # code...

   $colum1= generatePassword(5);
   $colum2 = serial_number(5);
   #write directly to csv file 

 }
0

2 Answers 2

2

Check this.

<?php

$words = array (
    array('One', 'Two', 'Three', 'Four'),
    array('1', '2', '3')
);

$fp = fopen('output_file.csv', 'w');

foreach ($words as $word) {
    fputcsv($fp, $word);
}

fclose($fp);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to write to a csv file there is the fputcsv function that can be used. But is depends on what you are writing and how you are organising the data. Here is a sample of code.

<?php

 $list = array (
   array('name', 'age', 'mobile'),
   array('Doe', 24, 65676767),
   array('Thomas', 67, 98765637),
 );

 $fp = fopen('file.csv', 'w');

 foreach ($list as $fields) {
    fputcsv($fp, $fields);
 }

 fclose($fp);
?>

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.