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.

MRDJR97
  • 818
  • 2
  • 10
  • 27
  • 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. – Shaun Bebbers May 19 '17 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)`? – Justinas May 19 '17 at 10:34
  • Also in your last `foreach()` loop, try it like this: `foreach ($rows as $rowIndex => $rowData) { echo '
    ' . print_r($rowIndex, 1) . ' => ' . print_r($rowData, 1) . '
    '; }` 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.
    – Shaun Bebbers May 19 '17 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? – MRDJR97 May 19 '17 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. – Shaun Bebbers May 19 '17 at 10:50
  • Please do some reading first; your PHP script is erroneous because you haven't thought things through or lack understanding of the language. I recommend http://php.net/manual/en/book.array.php - also even if you set up an array like this `$a[100][100] = null;` you'd still be able to set `$a[101][101] = 'Hello Mum';` after the fact. – Shaun Bebbers May 19 '17 at 10:55
  • Apologies, it is my first time using php. @ShaunBebbers noted, i changed that thank you. In the code you gave me I now have output of each row, but how do I divide each row up into seperate cells? I need to be able to access any value if I have the column no & row no. – MRDJR97 May 19 '17 at 10:55
  • That all depends on your data set. You have a text file that you are exploding by the opening bracket, but is this the best way? What does the text file contain? What's best is if, rather than loading a text file - which you know works - take a small [manageable] snippet of the text file and put it in the top like this: `$file = "('here','is')('my','example')";` then we'd be able to see the text that you're trying to process. And then you can ask specific questions and get better answers otherwise we're guessing as much as you are. – Shaun Bebbers May 19 '17 at 11:04
  • Does my answer on your other question regarding the same data solve this issue for you? http://stackoverflow.com/questions/44052754/how-to-explode-an-array-of-strings-and-store-results-in-another-array-php – mickmackusa May 22 '17 at 03:13

1 Answers1

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

Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81
  • if I do $a[1][1] I get the same result, so I dont think that's it – MRDJR97 May 19 '17 at 11:03
  • 1
    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 – Matteo Tassinari May 19 '17 at 11:07
  • 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. – Shaun Bebbers May 19 '17 at 12:31