Jump to content

PHP Export Array to CSV File Help


CamillaG

Recommended Posts

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

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/293012-php-export-array-to-csv-file-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.