CamillaG Posted December 10, 2014 Share Posted December 10, 2014 Hello, I have been assigned a PHP problem for my programming class. I'm a beginner so I am finding it really hard to figure out what to do. I got this far thanks to the internet. The problem is to take a list of students, place it in a multidimensional array, sort it, then divide it in half and place each half in two new files. I can't seem to figure out the last step. This is my code so far: <?php $students=array_map('str_getcsv', file('student.txt')); foreach ($student as $key => $row) $lastname [$key] = strtolower ($row[2]); array_multisort($lastname, SORT_ASC, $students); $groupone= array_slice($students,0,10); $grouptwo=array_slice($students,10); echo "<pre>" , print_r($groupone), "</pre>"; echo "<pre>" , print_r($grouptwo), "</pre>"; $firstgroup=fopen(firstgroup.txt, "w"); echo fwrite ($firstgroup,$groupone); fclose($firstgroup); At this point I get an error because I should use a string not an array to write the new file. I don't actually know how to create a new file and export the information onto it, this is me guessing. I would really really appreaciaite it if someone could help me out or point me in the right direction. Thank you so much, Camilla Quote Link to comment https://forums.phpfreaks.com/topic/293012-php-export-array-to-csv-file-help/ Share on other sites More sharing options...
Barand Posted December 10, 2014 Share Posted December 10, 2014 (edited) use foreach() loops to iterate through your group1 and group2 arrays. use fputcsv() to write to the output files. Edited December 10, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/293012-php-export-array-to-csv-file-help/#findComment-1499221 Share on other sites More sharing options...
CamillaG Posted December 10, 2014 Author Share Posted December 10, 2014 Dear Barand, I don't really know how to do that, could you please show me? Quote Link to comment https://forums.phpfreaks.com/topic/293012-php-export-array-to-csv-file-help/#findComment-1499224 Share on other sites More sharing options...
Barand Posted December 10, 2014 Share Posted December 10, 2014 $firstgroup=fopen('firstgroup.txt', "w"); foreach ($groupone as $row) { fputcsv($firstgroup, $row); } fclose($firstgroup); Quote Link to comment https://forums.phpfreaks.com/topic/293012-php-export-array-to-csv-file-help/#findComment-1499225 Share on other sites More sharing options...
CamillaG Posted December 10, 2014 Author Share Posted December 10, 2014 Oh alright! That makes sense, thank you so much Berend! However after I add that into my code in NetBeans, nothing really happens when I try to run it, how am I meant to create a new file? Or write into a new file? Quote Link to comment https://forums.phpfreaks.com/topic/293012-php-export-array-to-csv-file-help/#findComment-1499226 Share on other sites More sharing options...
Barand Posted December 10, 2014 Share Posted December 10, 2014 Opening a file with "w" mode creates the file. $firstgroup=fopen('firstgroup.txt', "w"); Quote Link to comment https://forums.phpfreaks.com/topic/293012-php-export-array-to-csv-file-help/#findComment-1499229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.