0

I currently have a text file that looks like this:

0000001
0000002
0000003
0000004
0000005
0000006....

I am using the code below to convert 1d array to 2d..

$filename  = "surface.txt";
$fp = fopen($filename, "r");
while(!feof($fp)) {  
     $content = fread($fp, filesize($filename));
     $lines = explode("\n", $content);
   for ($i=0; $i<=$row; $i++){
   for ($j =0; $j<=$col; $j++){
      //echo $lines[$i*$col + $j]; 
      $matrix[$i][$j] = $lines[$i*$col + $j];
      //var_dump($matrix[i][j]); 
     }
    }
   }
 fclose($fp);

My output array $matrix prints all NULL values.I have put var_dump() inside the loop to see what it displays.. Interestingly, It is reading all the values,but, after the last value, it kept on reading empty values too, which inturn made the $matrix empty(NULL) at last. I don't know how to fix this problem. If I am not clear in explaining how the output appears, please look at it below..

Array ( [0] => Array ( [0] => 0000001 ) )
Array ( [0] => Array ( [0] => 0000002 [1] => 0000002 ) )
Array ( [0] => Array ( [0] => 0000001 [1] => 0000002 [2] => 0000003 ) )

In the last lines, I see that the output is like below

  Array ( [0] => Array ( [0] => [1] => [2] => [3] => ) [1] => Array ( [0] => [1] => [2] =>   [3] => ) [2] => Array ( [0] => [1] => [2] => [3] => ) [3] => Array ( [0] => [1] => [2] => [3] => ) [4] => Array ( [0] => [1] => [2] => [3] => ) [5] => Array ( [0] => [1] => [2] => [3] => ) [6] => Array ( [0] => [1] => [2] => [3] => ) )

Any help is highly appreciated..

7
  • Where is $col defined? With the indexes for the for loops, you normally start on $i = 0 and the comparison is $i < $col, then you'll compare as many members as numbers in $col (you start by 0). Commented Feb 6, 2014 at 13:52
  • stackoverflow.com/questions/20728171/… try Commented Feb 6, 2014 at 13:54
  • $row and $col are defined in the original file,, I forgot to paste it..Sorry..$row=6; $col=3; Thanks for looking into it Commented Feb 6, 2014 at 13:54
  • Then check out my suggestion. If there are 6 rows, you want for ($i = 0; $i < $row; $i++). If you use the <= you'd have 7 rows, being the last one undetermined. Same with $col. Commented Feb 6, 2014 at 13:56
  • I did the correction and ran the code..it corrected the rows issue but, It is still dumping Null values. Commented Feb 6, 2014 at 13:59

2 Answers 2

0

Here is how I got it to work

  $filename="surface.txt"
 $fp = fopen($filename, "r");
 $content = fread($fp, filesize($filename));
 $lines = explode("\n", $content);
 for ($i=0; $i<$row; $i++){
 for ($j =0; $j<$col; $j++){

  $matrix[$i][$j] = $lines[$i*$col + $j];
 }
 }
Sign up to request clarification or add additional context in comments.

Comments

0
<?php

//asuming $row and $col are defined
//$row=5;
//$col=5;

$filename = "surface.txt";

$content = file_get_contents($filename);
$lines = explode("\n", $content);
foreach ($lines as $rowNum => $r) {
    $r = str_split($r);
    if ($rowNum + 1 > $row) {
        break;
    }
    if ($r === [""]) {
        continue;
    }
    while (count($r) > $col) {
        array_pop($r);
    }
    $matrix[] = $r;
}
var_dump($matrix);

7 Comments

After adding $lines=str_split($lines); my output is all NULL :(
I assume the file is raw data, OP did not mention any characters in it not to process.
the text file has all token numbers.. I need to put it into a 2D array to be able to access each of the token.
did my edit work? if you really don't wanna process all data, and only want to process $row number of rows and $col number of cols I can show you how to do that too
Could you please check and see if it works? any syntax errors?
|

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.