How to save an array in a file to access the array later with PHP?

I just want to quickly save the array that I get from the remote API so that I can talk to it on the local host.

So:

  • I currently have an array.
  • I want people to use an array without having to get it from the API.

There is no need for efficiency, this is not for the actual site, just to get some sanitation / formatting methods done, etc.

Is there a function like store_array() or restore_arrray() ?!

+46
php
Apr 18 2018-10-18T00:
source share
6 answers

The best way to do this is to serialize JSON. It is human readable and you will get better performance (the file will be smaller and faster to load / save). The code is very simple. Only two functions

Code example:

 $arr1 = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); file_put_contents("array.json",json_encode($arr1)); # array.json => {"a":1,"b":2,"c":3,"d":4,"e":5} $arr2 = json_decode(file_get_contents('array.json'), true); $arr1 === $arr2 # => true 

In this example, you can easily create your own store_array and restore_array functions.

For speed comparison, see the benchmark in Preferred Method for Storing PHP Arrays (json_encode vs serialize) .

+66
Apr 18 '10 at 13:12
source share

If you don't need the dump file for human reading, you can simply serialize() array.

storage:

 file_put_contents('yourfile.bin', serialize($array)); 

upon receipt:

 $array = unserialize(file_get_contents('yourfile.bin')); 
+31
Apr 18 '10 at
source share

Use serialize and unserialize

 // storing $file = '/tmp/out.data'; file_put_contents($file, serialize($mydata)); // $mydata is the response from your remote API // retreiving $var = unserialize(file_get_contents($file)); 

Or another, hacker way:

var_export () does exactly what you want, it will take any variable and save it in the view that the PHP parser can read back. You can combine it with file_put_contents to save it to disk and use file_get_contents and eval to read it.

 // storing $file = '/tmp/out.php'; file_put_contents($file, var_export($var, true)); // retrieving eval('$myvar = ' . file_get_contents($file) . ';'); 
+21
Apr 18 2018-10-18T00:
source share

You can use serialize to turn it into a string for writing to a file, and the accompanying unserialize to return it to an array structure.

I would suggest using a language-independent structure like JSON. This will allow you to upload files in languages ​​other than PHP, in case it will be possible later. json_encode to save it, and json_decode ($str, true) to return it.

+7
Apr 18 2018-10-18T00:
source share

Another quick way not mentioned here:

So add a header with <?php start, variable name \$my_array = with \$my_array = \$ and footer ?> End tag.

Now you can use include() like any other valid php script.

 // storing $file = '/tmp/out.php'; file_put_contents($file, "<?php\n\$my_array = ".var_export($var, true).";\n?>"); // retrieving as included script include($file); //testing print_r($my_array); 

out.php will look like this

 <?php $my_array = array ( 'a'=>1, 'b'=>2, 'c'=>3, 'd'=>4, 'e'=>5 ); ?> 
+2
Feb 18 '18 at 9:12
source share

Use php serialze:

 file_put_contents("myFile",serialize($myArray)); 
+1
Apr 18 2018-10-18T00:
source share



All Articles