PHP two Dimensional array in to file -
can please tell me how can save 2 dimensional array in text file
if have array unknown number of elements in index value:
$two_darray[unknown][unknown];
actually, @brad's , @hilmi's answers correct, i'm not sure using json advice.
you can choose
json
write: file_put_contents('somefile.json', json_encode($two_darray));
read: $two_darray = json_decode(file_get_contents('somefile.txt'));
xml
look this answer
serialized data
write: file_put_contents('somefile.txt', serialize($two_darray));
read: $two_darray = unserialize(file_get_contents('somefile.txt'));
csv (to use ms excel or db)
$handler = fopen('somefile.csv', 'w+'); foreach ($two_darray $one_darray) { fputcsv($handler, $one_darray); }
read fgetcsv
and sqlite
$db = new sqlitedatabase('somefile.sqlite'); foreach ($two_darray $one_darray) { $db->query("insert `mytable` values (".implode(',', $one_darray).")"); }
read: $two_darray = $db->sqlite_array_query("select * mytable");
edit binary (for .zip or .tar.gz)
try pack()
Comments
Post a Comment