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.
我只想快速存储一个从远程API获取的数组,这样我就可以在本地主机上乱搞它。
So:
所以:
- I currently have an array.
- 我目前有一个阵列。
- I want to people to use the array without having to get it from the API.
- 我希望人们使用该数组,而无需从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()
?!
有没有像store_array()或restore_arrray()这样的函数?
6 个解决方案
#1
65
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序列化。它是人类可读的,你将获得更好的性能(文件更小,加载/保存更快)。代码非常简单。只是两个功能
- json_encode
- json_encode
- json_decode
- 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.
您可以使用此示例轻松编写自己的store_array和restore_array函数。
For speed comparison see benchmark originally from Preferred method to store PHP arrays (json_encode vs serialize).
对于速度比较,请参阅最初从Preferred方法存储PHP数组的基准(json_encode vs serialize)。
#2
30
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'));
#3
21
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.
var_export()完全符合您的要求,它将采用任何类型的变量,并将其存储在PHP解析器可以读回的表示中。您可以将它与file_put_contents结合使用以将其存储在磁盘上,并使用file_get_contents和eval将其读回。
// storing
$file = '/tmp/out.php';
file_put_contents($file, var_export($var, true));
// retrieving
eval('$myvar = ' . file_get_contents($file) . ';');
#4
7
You can use serialize to make it into a string to write to file, and the accompanying unserialize to return it to an array structure.
您可以使用serialize将其转换为要写入文件的字符串,并使用随附的unserialize将其返回到数组结构。
I'd suggest using a language independent structure though, such as JSON. This will allow you to load the files using different languages than PHP, in case there's a chance of that later. json_encode to store it and json_decode($str, true)
to return it.
我建议使用与语言无关的结构,例如JSON。这将允许您使用与PHP不同的语言加载文件,以防以后有可能。 json_encode存储它和json_decode($ str,true)返回它。
#5
1
Use php's serialze:
使用php的序列号:
file_put_contents("myFile",serialize($myArray));
#6
1
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.
这样就添加了带有<?php开始标记的头部,变量名称为$ $ my_array =带有转义的\ $和页脚?>结束标记。
Now can use include()
like any other valid php script.
现在可以像任何其他有效的PHP脚本一样使用include()。
// 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
out.php看起来像这样
<?php
$my_array = array (
'a'=>1,
'b'=>2,
'c'=>3,
'd'=>4,
'e'=>5
);
?>
#1
65
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序列化。它是人类可读的,你将获得更好的性能(文件更小,加载/保存更快)。代码非常简单。只是两个功能
- json_encode
- json_encode
- json_decode
- 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.
您可以使用此示例轻松编写自己的store_array和restore_array函数。
For speed comparison see benchmark originally from Preferred method to store PHP arrays (json_encode vs serialize).
对于速度比较,请参阅最初从Preferred方法存储PHP数组的基准(json_encode vs serialize)。
#2
30
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'));
#3
21
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.
var_export()完全符合您的要求,它将采用任何类型的变量,并将其存储在PHP解析器可以读回的表示中。您可以将它与file_put_contents结合使用以将其存储在磁盘上,并使用file_get_contents和eval将其读回。
// storing
$file = '/tmp/out.php';
file_put_contents($file, var_export($var, true));
// retrieving
eval('$myvar = ' . file_get_contents($file) . ';');
#4
7
You can use serialize to make it into a string to write to file, and the accompanying unserialize to return it to an array structure.
您可以使用serialize将其转换为要写入文件的字符串,并使用随附的unserialize将其返回到数组结构。
I'd suggest using a language independent structure though, such as JSON. This will allow you to load the files using different languages than PHP, in case there's a chance of that later. json_encode to store it and json_decode($str, true)
to return it.
我建议使用与语言无关的结构,例如JSON。这将允许您使用与PHP不同的语言加载文件,以防以后有可能。 json_encode存储它和json_decode($ str,true)返回它。
#5
1
Use php's serialze:
使用php的序列号:
file_put_contents("myFile",serialize($myArray));
#6
1
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.
这样就添加了带有<?php开始标记的头部,变量名称为$ $ my_array =带有转义的\ $和页脚?>结束标记。
Now can use include()
like any other valid php script.
现在可以像任何其他有效的PHP脚本一样使用include()。
// 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
out.php看起来像这样
<?php
$my_array = array (
'a'=>1,
'b'=>2,
'c'=>3,
'd'=>4,
'e'=>5
);
?>