/    Sign up×
Community /Pin to ProfileBookmark

How can I store the contents of an array in a file?

Hi,
How can I store the contents of a multidimensional array in a file, and then, at some other time, read the stored values back into the original array?

It doesn’t matter to me whether I store the whole array or just the values or whatever, as long as it works.

I usually get good help in this forum, so thanks in advance for your help, guys.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 30.2005 — Hmmmm....I'm thinking this [b]might[/b] work, but I've not tested it:
[code=php]
# save array:
$saveData = var_export($theArray, TRUE);
$handle = fopen("the_file.txt", 'w') or die("can't open file");
fwrite($handle, $saveData);
fclose($handle);
# read array:
$saveData = file_get_contents("the_file.txt") or die("can't read file");
eval('$theArray = ' . $saveData . ';');
[/code]
Copy linkTweet thisAlerts:
@BluesManauthorNov 30.2005 — Thanks, NogDog,

I'll try it out when I get home from work (it's 16:25 here in Oslo, Norway and I'm about to go home ? ?.)
Copy linkTweet thisAlerts:
@bokehNov 30.2005 — Nogdog's method works fine. Here's another method. The two methods vary slightly in the way the array looks when stored in the text file.[code=php]#save array "$the_array"
$array_file = fopen("the_file.txt", 'w') or die("can't open file");
foreach($the_array as $k => $v) fwrite($array_file, "'$k' => '$v', n");
fclose($array_file);

# read array:
$the_array = array(file_get_contents("the_file.txt")) or die("can't read file");[/code]
Copy linkTweet thisAlerts:
@BluesManauthorNov 30.2005 — Hi,

I tried NogDog's suggestion, and it worked nicely - just the way I hoped it would - so I haven't looked into bokeh's way (yet).

Thanks a lot, guys. As usual you provide great help!
Copy linkTweet thisAlerts:
@SpectreReturnsDec 01.2005 — I would totally [b]serialize()[/b] it up.
Copy linkTweet thisAlerts:
@bokehDec 01.2005 — I would totally [b]serialize()[/b] it up.[/QUOTE]What, like this?[code=php]#save array "$TheArray"
$array_file = fopen("the_file.php", 'w');
fwrite($array_file, serialize($TheArray));
fclose($array_file);

# read array:
$TheArray = unserialize(file_get_contents("the_file.php"));[/code]


Also I have thought of a twist to Nogdog's method:[code=php]#save array "$TheArray"
$saveData = var_export($TheArray, TRUE);
$handle = fopen("the_file.php", 'w');
fwrite($handle, '<?php'."n".'$TheArray = '.$saveData.';'."n".'?>');
fclose($handle);

# read array
require("the_file.php");[/code]
×

Success!

Help @BluesMan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...