UPDATE: I realized that the issue was I was using print instead of echo to print the data, so it was showing the array instead of the data within it. Thanks a ton guys!
I currently have a text file that looks like this:
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
And I’m using this function:
function rFile($fileName){
$resultF = fopen($fileName, "r") or die("can't open file");
$array = array(); //Create the first dimension of a 2D array
$i=0;
while(!feof($resultF)){
$line = fgets($resultF);
$line = trim($line, "\n");
$tokens = explode(",",$line);
$array[$i]=array(); //Create the second dimension of the 2D array
$tokenCount = sizeof($tokens);
for($j=0; $j<$tokenCount; $j++){
$array[$i][$j] = $tokens[$j];
}
$i++;
}
return $array;
}
Essentially, it’s supposed to read through the file, explode each “0” and store it in a 2D array, $array. For some reason it returns this:
Array[0]
Array[1]
Array[2]
....etc etc
Anyone know what I did wrong?
You’re going about it the hard way, by using for loops and counters. By using PHP’s
$array[] = $valappend syntax, you can save a lot of effort here.Or to be even more concise: