PHP二维数组到文件中

时间:2022-03-29 21:36:37

Can you please tell me how I can save a two dimensional array in to a text file

你能告诉我如何将二维数组保存到文本文件中吗?

if I have an array with unknown number of elements in its index value:

如果我的索引值中有一个元素数量未知的数组:

$two_darray[unknown][unknown];

4 个解决方案

#1


3  

Actually, @Brad's and @Hilmi's answers are correct, but I'm not sure using only JSON is a good advice.

实际上,@ Brad's和@Hilmi的答案是正确的,但我不确定只使用JSON是一个很好的建议。

You can choose

你可以选择

JSON

JSON

write: file_put_contents('someFile.json', json_encode($two_darray));

写:file_put_contents('someFile.json',json_encode($ two_darray));

read: $two_darray = json_decode(file_get_contents('someFile.txt'));

读:$ two_darray = json_decode(file_get_contents('someFile.txt'));

XML

XML

Look this answer

看看这个答案

Serialized data

序列化数据

write: file_put_contents('someFile.txt', serialize($two_darray));

写:file_put_contents('someFile.txt',serialize($ two_darray));

read: $two_darray = unserialize(file_get_contents('someFile.txt'));

读:$ two_darray = unserialize(file_get_contents('someFile.txt'));

CSV (to use with MS Excel or some DB)

CSV(与MS Excel或某些数据库一起使用)

$handler = fopen('someFile.csv', 'w+');
foreach ($two_darray as $one_darray) {
    fputcsv($handler, $one_darray);
}

read it with fgetcsv

用fgetcsv读取它

and even SQLite

甚至是SQLite

$db = new SQLiteDatabase('someFile.sqlite');
foreach ($two_darray as $one_darray) {
    $db->query("INSERT INTO `MyTable` VALUES (".implode(',', $one_darray).")");
}

read: $two_darray = $db->sqlite_array_query("SELECT * FROM MyTable");

读:$ two_darray = $ db-> sqlite_array_query(“SELECT * FROM MyTable”);

EDIT Binary (for .zip or .tar.gz)

编辑二进制文件(.zip或.tar.gz)

Try pack()

试试pack()

#2


4  

You simply need to serialize this array. You can wrap it in serialize() and output this to a file. You can then use unserialize() later to decode it.

您只需要序列化此数组。您可以将其包装在serialize()中并将其输出到文件中。然后,您可以稍后使用unserialize()对其进行解码。

For portability, I recommend using JSON instead, with json_encode()/json_decode().

为了便于携带,我推荐使用JSON代替json_encode()/ json_decode()。

file_put_contents('someFile.json', json_encode($yourArray));

#3


0  

You can serialize the array to a string and save it into the file, and when ever you want that array just by unserializing the file's content you'll have the array back

您可以将数组序列化为字符串并将其保存到文件中,只要您通过反序列化文件的内容而想要该数组,就可以将数组恢复

#4


0  

<?php
$arr = array( array('aa', 'aa'), array('bb','bb') );
file_put_contents('/tmp/test',serialize($arr));
var_dump(unserialize(file_get_contents('/tmp/test')));
?>

You can also use json_encode/json_decode instead of serialize/deserialize as suggested by @Brad above

您也可以使用json_encode / json_decode代替序列化/反序列化,如上面@Brad所建议的那样

#1


3  

Actually, @Brad's and @Hilmi's answers are correct, but I'm not sure using only JSON is a good advice.

实际上,@ Brad's和@Hilmi的答案是正确的,但我不确定只使用JSON是一个很好的建议。

You can choose

你可以选择

JSON

JSON

write: file_put_contents('someFile.json', json_encode($two_darray));

写:file_put_contents('someFile.json',json_encode($ two_darray));

read: $two_darray = json_decode(file_get_contents('someFile.txt'));

读:$ two_darray = json_decode(file_get_contents('someFile.txt'));

XML

XML

Look this answer

看看这个答案

Serialized data

序列化数据

write: file_put_contents('someFile.txt', serialize($two_darray));

写:file_put_contents('someFile.txt',serialize($ two_darray));

read: $two_darray = unserialize(file_get_contents('someFile.txt'));

读:$ two_darray = unserialize(file_get_contents('someFile.txt'));

CSV (to use with MS Excel or some DB)

CSV(与MS Excel或某些数据库一起使用)

$handler = fopen('someFile.csv', 'w+');
foreach ($two_darray as $one_darray) {
    fputcsv($handler, $one_darray);
}

read it with fgetcsv

用fgetcsv读取它

and even SQLite

甚至是SQLite

$db = new SQLiteDatabase('someFile.sqlite');
foreach ($two_darray as $one_darray) {
    $db->query("INSERT INTO `MyTable` VALUES (".implode(',', $one_darray).")");
}

read: $two_darray = $db->sqlite_array_query("SELECT * FROM MyTable");

读:$ two_darray = $ db-> sqlite_array_query(“SELECT * FROM MyTable”);

EDIT Binary (for .zip or .tar.gz)

编辑二进制文件(.zip或.tar.gz)

Try pack()

试试pack()

#2


4  

You simply need to serialize this array. You can wrap it in serialize() and output this to a file. You can then use unserialize() later to decode it.

您只需要序列化此数组。您可以将其包装在serialize()中并将其输出到文件中。然后,您可以稍后使用unserialize()对其进行解码。

For portability, I recommend using JSON instead, with json_encode()/json_decode().

为了便于携带,我推荐使用JSON代替json_encode()/ json_decode()。

file_put_contents('someFile.json', json_encode($yourArray));

#3


0  

You can serialize the array to a string and save it into the file, and when ever you want that array just by unserializing the file's content you'll have the array back

您可以将数组序列化为字符串并将其保存到文件中,只要您通过反序列化文件的内容而想要该数组,就可以将数组恢复

#4


0  

<?php
$arr = array( array('aa', 'aa'), array('bb','bb') );
file_put_contents('/tmp/test',serialize($arr));
var_dump(unserialize(file_get_contents('/tmp/test')));
?>

You can also use json_encode/json_decode instead of serialize/deserialize as suggested by @Brad above

您也可以使用json_encode / json_decode代替序列化/反序列化,如上面@Brad所建议的那样