PDA

View Full Version : Creating Multidimensional Array


jtbrown57
10-22-2005, 10:43 PM
I am reading in a series of data from a file and wish to store it in a multidimensional array. The File looks like this...

Line1: Data1A Data1B Data1C Data1D
Line2: Data2A Data2B Data2C Data2D
Line3: Data3A Data3B Data3C Data3D
Line4: Data4A Data4B Data4C Data4D

I WANT my array to take each line and then separate by spaces so i have something like this...

Array[0][0] = Data1A
...
Array[3][3] = Data4D

I am actually using an array of 30 elements (inner array) and DONT know how many 30 element arrays I can have (the user can add them with a separate function). My problem is that nothing gets copied to my array. I am pretty sure its either how I instantiate the array or how I copy the elements from the exploded array.

What I have done so far:
$this_month = array( array( '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', '0', '0', '0', '0', '0', '0')
);

//$this_month = range(1,200);

$TheFile = "data_inc.txt";

$Open = fopen ($TheFile, "r");

if ($Open) {
//copying contents into content variable
$reports= file("$TheFile");
fclose($Open);

//exploding contents into '#' separated array
$j=0;

for ($i = 0; $i <= ((count($reports))-1); $i++) {
echo("<br><br>Each LINE<br><br>" . $reports[$i] . "<br>");

$each_rep = explode('#', $reports[$i]);

for ($l = 0; $l <= 30; $l++) {
if($l==0)
echo("<br><br>EXPLODED<br>");
echo($l .":".$each_rep[$l] . "<br>");
}

if ($each_rep[0] == $month_test && $each_rep[1] == $year_test){

$this_month[$j]= &$each_rep;
++$j;
}

}
echo("<br> HERE IS THE ARRAY");
for ($m = 0; $m <= ((count($this_month))-1); $m++) {
for ($n = 0; $n <= count($this_month[$m]); $n++) {
echo("M=". $m . " N=" . $n . " Data: " .$this_month[m][n] . "<br>");
}
echo("<br> Next Line");
}

}

Any help is appreciated! Thanks!