PHP Moving an array into different text files? -
i've been given list of data , need split , move different text files. i've tried few things far cant seem work.
<?php /* *order file based on surname (col 2) *create 2 new text files - class , b *split source text 2 equal lists *format them: id, firstname, lastname. words must first letter caps *move each list new file. *close files */ //this function converts every attribute/variable passed sentence case function uppercase($convert) { return ucwords($convert); } //this function compares 2 items see 1 higher //src: http://php.net/manual/en/function.usort.php function cmp($a, $b) { $compresult = strcmp($a[1], $b[1]); if ($compresult == 0) { return strcmp($a[2], $b[2]); }else { return $compresult; } } //this function gets rid of whitespace not needed function cut($c) { return trim($c, " \n\r\0"); } //open file $myfile = fopen("students.csv", "r"); echo "a"; //initialise array, giving them 'headers' $array = array(); echo "b"; //sort through data, moving multidimentional array , setting first letter in each item uppercase $i=0; while(!feof($myfile)){ $line = fgets($myfile); $pieces = explode(",", $line); $array[$i][0] = $pieces[0]; $array[$i][1] = cut(uppercase($pieces[2])); $array[$i][2] = cut(uppercase($pieces[1])); $i++; } echo "c"; //sort file second item in array usort($array, "cmp"); echo array_shift($array)."<br>"; echo "d"; //create class files $filea = fopen("class 1.txt", "w"); $fileb = fopen("class 2.txt", "w"); echo "e"; //get size of array $arraylength = count($array); //half array length( $half = ceil($arraylength /= 2); //echo $half; //echo $arraylength."</br>"; echo "f"; echo "<pre>"; print_r($array); echo "</br>"; //move first class text file $k = 0; foreach ($array $key){ echo $key[0]; if ($k < $half) { $current = file_get_contents($filea); $current .= $key; } } echo "g"; fclose($filea); fclose($fileb); fclose($myfile); echo "h";
when runs, following line recurring each item in array
warning: file_get_contents() expects parameter 1 valid path, resource given in c:\xampp\htdocs\phplabs\ee1600assignment.php on line 93
the document has 25 items this:
123, billy, bobs
any appreciated. thank you
file_get_contents
expects file path, providing file handler. want instead fgets($filea)
.
alternatively, if want read complete file (it's not entirely clear code), can use fread($filea)
.
Comments
Post a Comment