2016-11-10 4 views
2

Ich habe eine Liste von Daten erhalten, und ich muss es teilen und in verschiedene Textdateien verschieben. Ich habe bisher ein paar Dinge ausprobiert, aber es scheint nicht zu funktionieren.PHP Ein Array in verschiedene Textdateien verschieben?

<?php 

/* 
*order the file based on surname (col 2) 
*create two new text files - class A and B 
*split the source text into two equal lists 
*format them: ID, firstname, lastname. all words must be first letter caps 
*move each list to a new file. 
*close files 
*/ 

//This function converts every attribute/variable passed to it into a sentence case 
function Uppercase($convert) { 
    return ucwords($convert); 
} 

//This function compares two items to see which one is 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 the whitespace that is not needed 
function cut($c) { 
    return trim($c, " \n\r\0"); 
} 

//open file 
$myfile = fopen("students.csv", "r"); 

echo "A"; 

//initialise the array, giving them 'headers' 
$array = array(); 

echo "B"; 

//sort through the data, moving it to a multidimentional array and setting the first letter in each item to 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 the file by the second item in the 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 the array length(
$half = ceil($arraylength /= 2); 
//echo $half; 

//echo $arraylength."</br>"; 
echo "F"; 

echo "<pre>"; 
print_r($array); 
echo "</br>"; 

//move the first class into a text file 
$k = 0; 

foreach ($array as $key){ 
    echo $key[0]; 

    if ($k < $half) { 
     $current = file_get_contents($fileA); 
     $current .= $key; 
    } 
} 

echo "G"; 

fclose($fileA); 
fclose($fileB); 
fclose($myfile); 

echo "H"; 

Wenn dies ausgeführt wird, erhalten I die folgende Zeile für jedes Element wiederkehrenden in dem Array

Warning: file_get_contents() erwartet Parameter 1 ein gültiger Pfad zu sein, Ressourcen in C angegeben: \ xampp \ htdocs \ PHPLabs \ EE1600Assignment.php on line 93

Das Dokument selbst hat 25 Elemente, die wie folgt aussehen:

123, billy, bobs 

Jede Hilfe wird geschätzt. Danke

Antwort

0

file_get_contents erwartet einen Dateipfad, aber Sie stellen einen Dateihandler zur Verfügung. Sie möchten wahrscheinlich stattdessen fgets($fileA).

Wenn Sie alternativ die vollständige Datei lesen möchten (aus Ihrem Code ist dies nicht eindeutig), können Sie fread($fileA) verwenden.

+0

Für FileA und FileB einen fopen-Call, alles, was ich tun möchte, ist, die Daten, um es aus dem Array zu schreiben. Ich denke, das ist die erste Antwort, die Sie vorgeschlagen haben –

0

Gemäß der Datei documentation erfordert file_get_contents einen Pfad zu der Datei, die Sie öffnen möchten (gemäß der Fehlermeldung, die Sie erhalten - file_get_contents() expects parameter 1 to be a valid path).

Sie vorbei in $fileA - die zuvor erstellt werden

fopen("Class 1.txt", "w");