1

I have a multidimensional array, currently it's only printing the array on the page. I want to be able to save it in a flat text file storing the selected checkbox information when the user submits it. There are 6 days in total.

5
  • 7
    serialize it Commented Aug 2, 2013 at 11:39
  • 2
    @CarlosCampderrós or better yet json_encode it, so you can easily read it in other languages, too Commented Aug 2, 2013 at 11:44
  • file_put_contents('path/to/your/file', json_encode($_POST)); Commented Aug 2, 2013 at 11:45
  • you can use json_encode Commented Aug 2, 2013 at 11:46
  • Which is the purpose of that storage? Its for serialization, or want to distribute it, so others users can consume? Commented Aug 2, 2013 at 11:59

3 Answers 3

13

I'd simply do this:

<?php
    if ($_POST)
    {//always check, to avoid noticed
        file_put_contents('theFilenameToWriteTo.json', json_encode($_POST));
    }
?>

The benefit of json_encode is that it's more or less standard. All languages that I know of can parse this format. If you're using node.js, it can read the data, Java? JSON-java is there for you. Python? import json, hell, even C++ libs are easy to find.
To reuse that data in PHP:

$oldPost = json_decode(file_get_contents('theFileNameToRead.json'));
//$oldPost will be an instance of stdClass (an object)
//to get an assoc-array:
$oldPostArray = json_decode(file_get_contents('sameFile.json'), true);

There are other options to: serialize and write, followed by unserialize on read.
If you want that array to be copy-pastable into existing PHP code, use var_export

file_put_contents('theArray.txt', var_export($_POST, true));

If you open the file then, it'll contain an array as though it were written by hand:

array (
  0 => 1,
  1 => 2,
  2 => 
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)

As Carlos Campderrós pointed out, you can even include strings generated by var_export. It's important to note that var_export doesn't handle circular references, though, but seeing as you're using a $_POST array, that's not an issue here
To recap, a list of useful functions:

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

1 Comment

+1 I was writing practically the same, you have saved me a lot of work ;-). Just add that you can read the var_export'ed code with an include or require :D
1

You could use the

print_r($multiArray,true)

The true means we want to capture the output and not print.

Array (PHP 5.4 )

$multiArray = [
        0 => [ 'a' => 'b'],
        1 => [ 'c' => 'd', 
               'e' => [
                   'f' => 'g',
                   'h' => 'i'
               ]
        ],
];

PHP

$file = 'array.txt';

$fh = fopen($file, 'w') or die("can't open file");  

fwrite($fh, print_r($multiArray,true));

fclose($fh);

Or you could use the serialize() and unserialize() if you want to use the array later

fwrite($fh, serialize($multiArray) );

Comments

0

Use json_encode(), convert the array into string and store it. You can also convert back from string to array using json_decode().

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.