12

I have An array which looks like following after using print_r

Array ( [0] => Array ( [0] => piklu [name] => piklu ) [1] => Array ( [0] => arindam [name] => arindam ) [2] => Array ( [0] => shyamal [name] => shyamal ) [3] => Array ( [0] => arko [name] => arko ) [4] => Array ( [0] => pamela [name] => pamela ) [5] => Array ( [0] => dodo [name] => dodo ) [6] => Array ( [0] => tanmoy [name] => tanmoy ) [7] => Array ( [0] => jitu [name] => jitu ) [8] => Array ( [0] => ajgar [name] => ajgar ) ) 

Now I want to write this array direct to a file, I use the file_put_contents method, but I don't know how to get the data from the file exactly how they looks like original. Any idea to solve this?

2
  • how you save it to file? can you show your code also?
    – user1646111
    Feb 14, 2013 at 6:40
  • why you want to write this array to file? and why you want to pick up it from file? describe your objective of work?
    – Ripa Saha
    Feb 14, 2013 at 6:40

5 Answers 5

33

Your problem at the moment is basically that you're only able to write strings into a file. So in order to use file_put_contents you first need to convert your data to a string.

For this specific use case there is a function called serialize which converts any PHP data type into a string (except resources).

Here an example how to use this.

$string_data = serialize($array);
file_put_contents("your-file.txt", $string_data);

You probably also want to extract your data later on. Simply use unserialize to convert the string data from the file back to an array.

This is how you do it:

$string_data = file_get_contents("your-file.txt");
$array = unserialize($string_data);
1
  • 1
    Thanx..this fulfill my goal exactly...Thankx again a lot. Feb 14, 2013 at 6:57
8

Here are two ways:

(1) Write a JSON representation of the array object to the file.

$arr = array( [...] );
file_put_contents( 'data.txt', json_encode( $arr ) );

Then later...

$data = file_get_contents( 'data.txt' );
$arr = json_decode( $data, true );

(2) Write a serialized representation of the array object to the file.

$arr = array( [...] );
file_put_contents( 'data.txt', serialize( $arr ) );

Then later...

$data = file_get_contents( 'data.txt' );
$arr = unserialize( $data );

I prefer JSON method, because it doesn't corrupt as easily as serialize. You can open up the data file and make edits to the contents, and it will encode/decode back without big headaches. Serialized data cannot be changed so easily or corrupted, or unserialize() won't work. Each variable is defined by type and length, and these values must be updated along with the actual change you are making.

1
  • 2
    The only "problem" with PHP & Json is you need to handle array's correctly. PHP doesn't differentiate between a list and an hash/object (like javascript/json), in PHP you only have an Array. So you probably want to the a look the the second parameter of json_encode() to get it right. Feb 14, 2013 at 6:53
4

file_put_contents writes a string to a file, not an array. http://php.net/manual/en/function.file-put-contents.php

If you'd like to write what you see there in that print_r to a file, you can try this:

ob_start();
print_r($myarray);
$output = ob_get_clean();
file_put_contents("myfile.txt",$output);
4
  • 2
    instead of using output buffering, take a look at the second parameter of print_r Feb 14, 2013 at 6:44
  • Good point. I know of that but I thought that was added in 5, looks like it was added in 4.3. Either way, without knowing the PHP version, better to play it safe! Feb 14, 2013 at 6:49
  • 1
    I'd say at this point of time most user have at least PHP 5.3. I think it's always important to at least mention the new features so that users quickly adopt them and update their PHP so the language can move faster. Feb 14, 2013 at 6:55
  • +1 for print_r($arr, true). Although, it doesn't help much when trying to restore the file contents to a valid array.
    – illuzive
    Feb 14, 2013 at 7:46
2

If this is static content, a more performant way to do this would be:

$filePath = 'path/to/file.php';

//Saving
file_put_contents($filePath, '<?php '.PHP_EOL.'return '.var_export($array, true).';');
 

//Retrieving data
$array = include $filePath;

This will also be saved in OPcode cache and can even be pre-loaded. However, if you use OPcode cache or pre-loading this will not reload between deploys.

1

i m not sure but maybe its something like this. You want to serialize() the array on writting. it will put your array into test.txt

file_put_contents('test.txt', serialize($array));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.