0

I am trying to use files to hold an array for checkers

this is the array

$board = array(
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0)
   );

while also giving the values so that i can set the beginning of the board with the pieces placed in a predefined positions to start the game in then have them user input which location they want to move the pieces into

I already have this while loop

      $row = 0;
    print "<form>";
    print "<table border = 1>";
    while ($row < 8){ // Counts to 8. (from 0...7 = 8 times. 0 ... 8 = 9 times)
       print "<tr>";
       $row++;
       $col = 0; // reset column to 0 each time printing one row.

       while ($col < 8){
        print "<td>";
        if($Board[$row][$col] == 0)
        {
            $value=$row.$col;
            print "<input type=\"checkbox\" name=\"box[]\" value=\"$value\">";
            // Add \ before " otherwise it will treat as the end of the quote.

        }
        print "</td>";
        $col++;

       }

       print "</tr>";

    }
    print "</table>";
    print "</form>";

}

2
  • what I am trying to find out is what php functions do i need to make this array store in the file then display board into html tables with 8x8 rows and columns. Commented Nov 4, 2010 at 21:08
  • I just want to be pointed in the right direction and I don't expect anyone to this for me Commented Nov 4, 2010 at 21:14

4 Answers 4

10
file_put_contents($f, serialize($board));

this will serialize your multidimensional array in a file.

To read it back, use

$board = unserialize(file_get_contents($f));
Sign up to request clarification or add additional context in comments.

Comments

4

2 variants: using serialize

#dump:
file_put_contents('file_name', serialize($board));
#restore:
$board=unserialize(file_get_contents('file_name'));

using JSON:

#dump:
file_put_contents('file_name', json_encode($board));
#restore:
$board=json_decode(file_get_contents('file_name'));

JSON variant works faster, but can dump only simple structures (strings, arrays, numbers). serialize can dump objects too but works slower and generate more output

7 Comments

JSON = JavaScript Object Notation, so why would it not be able to dump objects?
@BoltClock: not of specific classes you get back after decoding, only stdClass, which may not be what you want...
@Wrikken - "not of specific classes you get back after decoding, only stdClass, which may not be what you want" what did you mean by this comment towards @BoltClock about JSON
That:class Foo{}; echo get_class(json_decode(json_encode(new Foo()))); results in stdClass and not in Foo (in other words: the type of object is not preserved in JSON (it's just not in the JSON spec).
so using JSON on this example isn't good or is it I have never used it before
|
1

why don't you serialize the array and store it as a string into the file. to get the array back, you can read the string from the file and un-serialize it. have a read here here

Comments

0

Why serialize and store ? Even you can directly print array to file

ob_start();

print_r( $yourArry);

$output = ob_get_clean();

file_put_contents( 'yourfilename.txt',$output );

See example here http://www.my-php-scripts.net/index.php/Array-Scripts/php-write-multidimensional-array-into-file-advanced-php.html

Comments

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.