Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store an array in a file to access as an array later with PHP?

Tags:

php

I just want to quickly store an array which I get from a remote API, so that i can mess around with it on a local host.

So:

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

There are no needs for efficiency etc here, this isnt for an actual site just for getting some sanitizing/formatting methods made etc

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

like image 695
Haroldo Avatar asked Apr 18 '10 12:04

Haroldo


People also ask

How do I store an array into a file?

LiveCode enables you to convert an array into a format that can be stored in a file or sent to a URL using arrayEncode. Conversely, you can use arrayDecode to to convert data back into a LiveCode array.

How do I permanently store data in an array?

Solution 1 By principle, array list exist only as long as program is running, when program stops, array is killed. The only way to make its contain permanent is to store the array in a data file and reload the array from file when program is launch again.

How do you store array values in a variable?

Storing Data in Arrays. Assigning values to an element in an array is similar to assigning values to scalar variables. Simply reference an individual element of an array using the array name and the index inside parentheses, then use the assignment operator (=) followed by a value.

Can you throw an array instead of a string as an exception in PHP?

Yes, you can.


4 Answers

The best way to do this is JSON serializing. It is human readable and you'll get better performance (file is smaller and faster to load/save). The code is very easy. Just two functions

  • json_encode
  • json_decode

Example code:

$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 

You can write your own store_array and restore_array functions easily with this example.

For speed comparison see benchmark originally from Preferred method to store PHP arrays (json_encode vs serialize).

like image 70
retro Avatar answered Sep 20 '22 08:09

retro


If you don't need the dump file to be human-readable, you can just serialize() the array.

storing:

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

retrieving:

$array = unserialize(file_get_contents('yourfile.bin')); 
like image 27
soulmerge Avatar answered Sep 22 '22 08:09

soulmerge


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, hacky way:

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

// storing
$file = '/tmp/out.php';
file_put_contents($file, var_export($var, true));

// retrieving
eval('$myvar = ' . file_get_contents($file) . ';');
like image 31
K. Norbert Avatar answered Sep 20 '22 08:09

K. Norbert


Another fast way not mentioned here:

That way add header with <?php start tag, name of variable \$my_array = with escaped \$ and footer ?> end tag.

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

<?php
  // storing
  $file = '/tmp/out.php';
  $var = ['a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5];

  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
  );
?>
like image 32
MTK Avatar answered Sep 20 '22 08:09

MTK