/    Sign up×
Community /Pin to ProfileBookmark

Dump array contents to a text file

What’s the quickest way to dump the contents of an array into
a text (.php) file.

$file = “database.php”;
$fh = fopen($file,’w’);

//function to dump the info into
the database.php file with a single
line break after each element

fclose($fh);

can i get a little help with a quick function
to do that for me?

maybe a for loop or foreach or array_walk?

thank guys

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@CharlesJan 16.2007 — We can shorten it even more; there's not need to open the file first.
[center]http://www.php.net/manual/en/function.file-put-contents.php[/center]
Copy linkTweet thisAlerts:
@NogDogJan 16.2007 — We can shorten it even more; there's not need to open the file first.
[center]http://www.php.net/manual/en/function.file-put-contents.php[/center]
[/QUOTE]

Unless you need to be PHP4-compatible.
Copy linkTweet thisAlerts:
@NogDogJan 16.2007 — [code=php]
$file = "database.php";
$fh = fopen($file,'w');
fwrite($fh, implode("n", $myArray));
fclose($fh);
[/code]
Copy linkTweet thisAlerts:
@bokehJan 16.2007 — Hmm! Are we all reading the same question?[code=php]<?php

# start: for test purposes
$my_array = array('a','b','c','d','e');
# end: for test purposes

$to_put_in_file = "<?phpn$my_array = ".var_export($my_array, true).";n?>";

# start: for test purposes
header('Content-Type: text/plain');
echo $to_put_in_file;
# end: for test purposes
?>[/code]
Or you could use [I]serialize()[/I].
Copy linkTweet thisAlerts:
@NogDogJan 16.2007 — Hmm! Are we all reading the same question?...[/QUOTE]
I don't think so:
...//function to dump the info into

the database.php file with a single

line break after each element...[/quote]

I read this to mean that if [b]$myarray = array('a', 'b', 'c');[/b] then the output to the text file would simply be:
a
b
c
...which is what I was trying to accomplish.
Copy linkTweet thisAlerts:
@NightShift58Jan 16.2007 — to dump the contents of an array into a text (.php) file.[/quote]Semantics perhaps... but why just write the value of an array into a .php file?

If I combine the words "dump" (as in SQL dump) and ".php", something tells me the OP has greater designs... IF "dumping" a SQL table meant just writing the contents of the table fields, the dump would have little use.

If the resulting file is not going to be used as a PHP file (i.e. included somewhere), the alternative output could be print_r() or var_dump().

I guess bsmbahamas will have to play King Solomon..
Copy linkTweet thisAlerts:
@bsmbahamasauthorJan 16.2007 — I don't think so:

I read this to mean that if [b]$myarray = array('a', 'b', 'c');[/b] then the output to the text file would simply be:
a
b
c
...which is what I was trying to accomplish.[/QUOTE]



I'm trying to get what NogDog is saying above.

if the array is $myarray = array('a','b,'c');

dump it into a database.php file with a line break after each item.


basically i gonna use file($file) to read the contents of database.php

into an array, edit parts of the array then return it to the database.php

file in the same format as before but after updates are done.


so if i wanted to update line 3 in database.php,

i'd read in into an array, do the updates on line 3,

then overwrite the original database.php with the

updated info.

$file = "database.php";

$data = file($file);

//update line 3

do update to $data[2];

then i normally use a loop to update it ...

$fh = fopen($file,'a');

for( $i = 0; $i < sizeof($data); $i++ ){

$data[$i] = trim($data[$i]);

$content = "$data[i]n";

fwrite($fh,$content);

}

fclose($fh);



but i have to open the file

trim each line

add a break and append each line

then close the file



i find that if the line has a number it doesn't need to be trimmed,

but if the line is a word then it needs to be trimmed, so i trim

each line then add a single break before appending it back

to the file.



but there must be an easier way to do the updates

and then just dump them to the database.php file

in one swoop rather than looping and trimming and appending



hope that clears it up ...



seems like this may do the trick but i need to go test it ...



$file = "database.php";

$fh = fopen($file,'w');

fwrite($fh, implode("n", $myArray));

fclose($fh);
Copy linkTweet thisAlerts:
@bsmbahamasauthorJan 16.2007 — $file = "database.php";

$data = file($file);

//simple update

$data[2] = "test line 3";

//delete old file

unlink($file);

//then create a new file in append mode

$fh = fopen($file,'a');

for( $i = 0; $i < sizeof($data); $i++ ){

//trim each line

$data[$i] = trim($data[$i]);

//add a single line break after each line

$content = $data[$i] . "n";

fwrite($fh,$content);

}

fclose($fh);

echo "<p>Done</p>";
Copy linkTweet thisAlerts:
@The_Little_GuyJan 16.2007 — I don't know if this is what you want:

[code=php]
<?
$handle = fopen("file.php", "w");
$text = array('a','b','c','d','e');
foreach($text as $value){
$insert .= implode(" ",$value);
}
if(fwrite($handle, $insert) == FALSE){
$_SESSION['error'] = '<span class="redtxt">There was an error</span>';
}else{
$_SESSION['error'] = '<span class="redtxt">File edited successfully</span>';
}
fclose($handle);
?>
[/code]
Copy linkTweet thisAlerts:
@NightShift58Jan 17.2007 — Try it this slight modification - though functionally the same - of NogDog's post:[code=php]<?php

$file = "database.php";
$data = file($file);

//simple update
$data[2] = "test line 3";

//then create a new file in overwrite mode
if ($fh = fopen($file,'w')) {
fwrite($fh,implode("n", $data));
fclose($fh);
echo "<p>Done</p>";
} else {
echo "<p>Problems</p>";
}
?>[/code]
You don't need to "unlink()", just overwrite the file when you're ready to roll. You don't need to cycle through the input array, implode() will do that for you. This is about the shortest and tightest it gets, including some form of error checking.
×

Success!

Help @bsmbahamas 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.18,
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,
)...