0

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.

9
  • Why not $a = []; or $arrayContents = array(); - I don't see where your $r and $c variables 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. Commented May 19, 2017 at 10:33
  • No scope issue here, because for and foreach does not add it's own scope. Where is $c and $r declared (in first $a[$r][$c]? Also you say that file has 1903 rows, but you loop for 9103 times. Why not $r < count($rows)? Commented May 19, 2017 at 10:34
  • Also in your last 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 $c and $r and give your variable and object names something more meaningful. $c I assume means 'column' but it could mean anything. Commented May 19, 2017 at 10:36
  • @ShaunBebbers that's what I originally did, but I thought that might've been issue so I change to $a[$r][$c], doesn't affect the code at all. If i do $a=[ ]; where will I initialise $r/$c or do they have to be? Commented May 19, 2017 at 10:46
  • @DanielRegan - but the variables $r and $c are 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 $a as a PHP array, which will have zero or more elements later on in the script. Commented May 19, 2017 at 10:50

1 Answer 1

0

At the end your script, executed "as is", $c will be 103 but elements in $a[1] will only be set from 0 to 102.

So your accessing a non-existant index, hence it prints nothing.

I have created a slightly modified example which clearly shows the problem.

Notice: Undefined offset: 103 in [...][...] on line 15

Sign up to request clarification or add additional context in comments.

3 Comments

if I do $a[1][1] I get the same result, so I dont think that's it
Your script depends on the contents of a file we don't know, so without that info it's impossible to fully reproduce your script
I agree with @MatteoTassinari - please show us the data that you are dealing with somehow. May be a PasteBin link or something? Or take a small snippet of it and put that in as a test case. Until you do we're all guessing as much as you are. Also explain what you are trying to achieve in the first instance. We only know that something doesn't work but without a fuller picture then no one will be able to help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.