Jump to content

Saving double array to a file? $matrix[10][10]


Recommended Posts

so I've figured out how to read an array and place it into a file with a single array...$matrix[10] and have it save onto a file all of the data with a new line per databut it gets kind of unnecessary and messy, however it's still readable. When it comes to $matrix[10][10] it becomes UNREADABLE, basically what I do isrun it through [0][0-10] and then [1][0-10] etc until its done all the values which isfine because its reparsable but for convenience I want it so I can quickly edit the numberson the txt file. ex:1,1,2,3,5,6,7,2,4,62,4,5,6,7,8,5,3,1,3 instead of112356724624...etc I want some form of code that can easily parse onto a text file and read it, I haven't posted my code because I like to implement the code in myself to have it work efficiently Here's what I have tried which has...failed..

$matrix [10][10];if(isset($_POST['mapname'])){$file = fopen('file.txt', 'w');foreach($matrix as &$array){fwrite($file, $array ."\n". '');}}

P.s. Excuse my informality, I've been quite stomped and I apologize for the lack of an introduction thread. Edit: Changed the first line, it was creating a syntax error

Edited by Monckey100
Link to post
Share on other sites

I am thinking something like this will work for you

 for($i = 0; $i < sizeOf($matrix); $i++) {$line = "";	for($x = 0; $x < sizeOf($matrix[0]); $x++) {	  $line .= $matrix[$i][$x] . ($x < sizeOf($matrix[0]) ? "," : "\n")  ; // edit 2	}  fwrite($file, $line);}

edit: fixed a syntax error

Edited by astralaaron
  • Like 1
Link to post
Share on other sites
$matrix[10][10] = "hi";$file = fopen('file.txt', 'w');if(isset($_POST['mapname'])){for($i = 0; $i < sizeOf($matrix); $i++) {$line = "";	    for($x = 0; $x < sizeOf($matrix[0]); $x++) {		  $line .= $matrix[$i][$x] . ($x < sizeOf($matrix[0]) ? "," : "\n")  ; // edit 2	    }  fwrite($file, $line);}}

The code looks like this but it yields 0 bytes the text file....here's the rest of the code that is being used to put values into the matrix.

<form method="post" action="<?php echo $PHP_SELF;?>"><table><tr><td>Item tree:</td><td><INPUT TYPE="Text" name="items"></td></tr><tr><td>Map Name:</td><td><INPUT TYPE="Text" name="mapname"></td><td><INPUT TYPE="submit" value="save"/></td></tr></table><table border=1 width=100%><?php$matrix[10][10] = "hi";$file = fopen('file.txt', 'w');if(isset($_POST['mapname'])){for($i = 0; $i < sizeOf($matrix); $i++) {$line = "";	    for($x = 0; $x < sizeOf($matrix[0]); $x++) {		  $line .= $matrix[$i][$x] . ($x < sizeOf($matrix[0]) ? "," : "\n")  ; // edit 2	    }  fwrite($file, $line);}}for ($i = 0; $i <= 10; $i++){echo'<tr>';for($r =0; $r <= 10; $r++){  echo'<td><INPUT TYPE="Text" name="$matrix[$i][$r]" value="empty" size="10%"></td>';}echo'</tr>';}?>

Link to post
Share on other sites

I did this test and the array loops out like you are trying to do. Make sure that $_POST['mapname'] is set so it runs that part of the code.

<?php $matrix = array( array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01) );  for($i = 0; $i < sizeOf($matrix); $i++) {$line = "";$size = sizeOf($matrix[$i]);			for($x = 0; $x < $size; $x++) {				  $line .= $matrix[$i][$x] . ($x < ($size-1) ? "," : "\n")  ; // edit 2			}  echo($line);}?>

Edited by astralaaron
  • Like 1
Link to post
Share on other sites
I did this test and the array loops out like you are trying to do. Make sure that $_POST['mapname'] is set so it runs that part of the code.
<?php $matrix = array( array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01),	 array(10, 10, 01, 03, 04, 01, 03, 05, 02, 01) );  for($i = 0; $i < sizeOf($matrix); $i++) {$line = "";$size = sizeOf($matrix[$i]);			for($x = 0; $x < $size; $x++) {				  $line .= $matrix[$i][$x] . ($x < ($size-1) ? "," : "\n")  ; // edit 2			}  echo($line);}?>

It doesn't seem to parse as intended, instead it goes xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxinstead ofxxxxxxxxxxxxxxxxxxxxxxxx but I guess it's not that big of a deal, any idea how I would go about reparsing it back into the matrix?
Link to post
Share on other sites
It doesn't seem to parse as intended, instead it goes xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxinstead ofxxxxxxxxxxxxxxxxxxxxxxxx
If you look at the page source code it will look like the map grid, the \n doesn't show on the page edit: to load it back into the array you can just do explode("\n",$lines), and then explode(",",$line) Edited by astralaaron
  • Like 1
Link to post
Share on other sites
If you look at the page source code it will look like the map grid, the \n doesn't show on the page edit: to load it back into the array you can just do explode("\n",$lines), and then explode(",",$line)
okay great, I opened it in wordpad,notepad and microsoft word, the \n's don't seem to be registering
Link to post
Share on other sites
How would I go about reading the file now? I understand I wouldn't just place an explode...
I am pretty sure that you can just explode by "\n" you will have the array of the lines, then do a loop on that array and explode by the ","
Link to post
Share on other sites

You can also use implode to write it.

for($i = 0; $i < count($matrix); $i++) {  echo implode(',', $matrix[$i]) . "\n";}

To recreate the array, explode by "\n" and then explode each line by ",". You can also serialize and unserialize the array. file_put_contents('file.txt', serialize($matrix));$matrix = unserialize(file_get_contents('file.txt')); http://www.php.net/manual/en/function.serialize.php

  • Like 2
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...