My code:
<?php
$file="./Speed10.txt";
$document=file_get_contents($file);
$rows = explode ('(', $document);
$a[$r][$c];
for($r=0; $r<9103; $r++){ //1903 rows in text file
$a[$r][$c]; // Array declared here - doesnt solve problem
for($c=0; $c<103; $c++){
//$a[$r][$c] = rand();
// print_r($a[$r][$c]);
}
}
foreach ($rows as $ri => $row) {
$a[$ri][$c] = explode (';', $row);
//XXXXXXXXXXXXX
}
print_r($a[1][$c]); // NOT PRINTING*
?>
I have a 2D array as you can see above, it divides a text file into rows and columns. That part works perfectly, but I try to print out all the cells of one row, it isn't printing.
However, if I move the print_r line to where the X's are, it works (although its being printed out in a loop). Sounds like a scope problem to me but I can't figure out what. I tried initialising the array as a global variable but that didn't fix it.
$a = [];or$arrayContents = array();- I don't see where your$rand$cvariables are being set, but in PHP, you may initialise an array as I have described. You don't need to say how big it is.forandforeachdoes not add it's own scope. Where is$cand$rdeclared (in first$a[$r][$c]? Also you say that file has1903rows, but you loop for9103times. Why not$r < count($rows)?foreach()loop, try it like this:foreach ($rows as $rowIndex => $rowData) { echo '<pre>' . print_r($rowIndex, 1) . ' => ' . print_r($rowData, 1) . '</pre>'; }You'll be able to see each element. Initialise values for$cand$rand give your variable and object names something more meaningful.$cI assume means 'column' but it could mean anything.$rand$care not declared before the non-declaration of the array. So it's not doing anything. It's a non-working line of PHP scripting that could confuse the issue.$a = array();- at least PHP devs know what you're trying to achieve, to set up a resource called$aas a PHP array, which will have zero or more elements later on in the script.