限制在PHP中写入csv文件的1D关联数组的行数

I have a large number of one dimensional arrays stored in memory cache and I want to write them to a CSV files, the arrays come one by one through the use of a queue. But I want to "limit each csv file to about 100 rows" and then write the remaining arrays into newer files and so on. I would highly appreciate any help in this. I used this function to pass arrays into a csv but I dont know how to limit the number of rows to 100 and then open new files and write to them. The messages passed in the queue are keys named as SO1,SO2, SO3 n so on with the last message being passed as "LAST". And based on the keys , the arrays associated with the keys are read in from memcache and have to be written into csv files. The messages reach one after another via rabitmq queue from a preceeding module. // Assuming $S01 is an array fetched from memcache based on the key say S01 received via a queue. $SO1 = array('Name'=> 'Ubaid', 'Age'=>'24', 'Gender'=>'Male','Lunch'=>'Yes', 'Total'=> '1000');

$row_count= 0;
$csv_file_count= 1;


while($msg != "LAST" ){  // As long as msg recieved is not LAST
$csv = fopen("file_". $csv_file_count.".csv", "w");
    fputcsv($csv, array_keys($SO));
    while($msg != "LAST" && $row_count<100){                    
        fputcsv($csv, $SO);         // Write toCSV
        $row_count++;
    }
    $row_count=0;                                   
    $csv_file_count++;                                              
    fclose($csv);

You could make a counter like this.

$row_count = 0;
$csv_file_count = 1;


while (!$q->isEmpty()){            // as long as the queue is not empty
    $csv = fopen("file_".$csv_file_count.".csv", "w");   // open "file_n.csv" n file number
    fputcsv($csv,explode(',',"col1,col2,col3,col4"));    // Your custom headers
    while (!$q->isEmpty() && $row_count < 100){          // so while queue is not empty and the counter didnt reach 100
        fputcsv($csv, explode(',',$q->pop()));       // write to file. Explode by , or space or whatever your data looks like
        $row_count++;                  // increment row counter
    }
    $row_count = 0;            // when that is not true anymore, reset row counter
    $csv_file_count++;         // increment file counter
    $csv.close()           // close file
}  // repeats untill queue is empty

Updated to use fputcsv()

If you want another seperator in your csv file you can do like this:

fputcsv($csv, explode(',',explode(',',$q->pop()),";"); // (;) for example. Default is comma (,)

You can also specify a field enclosure

fputcsv($csv, explode(',',explode(',',$q->pop()),",","'"); // (') for example. Default is double quote (")

fputcsv() takes 2 required parameters and 2 optional

From php.net fputcsv

int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\" ]]] )

Fields shall be an array, therefore

explode(',',$q->pop())
as the 2nd parameter