I have an array $urls_array
, how do I only save the contents and not anything else into a file?
INPUT:
Array (
[0] => "http://google.com"
[1] => "http://facebook.com"
[2] => "http://yahoo.com"
)
OUTPUT:
http://google.com
http://facebook.com
http://yahoo.com
I tried using json_encode($urls_array)
and serialize()
and print_r()
, but nothing gave me the clean result I wanted. Any help?
Have you tried file_put_contents?
file_put_contents('filename', join("
", $your_array));
The above has only a small problem: if your array is big, it will be converted to a long string before being written to the file as a whole. To avoid this memory intensive operation, loop through the array and write each item to the file sequentially:
if(($f = fopen("filename","w")) !== FALSE) {
array_walk($your_array, function($item) use($f) { fwrite($f, $item . "
"); });
// or, with an implicit loop
// foreach($your_array as $item) fwrite($f, $item . "
");
}
try this code its 100% working...
<?php
$data=array("http://google.com","http://facebook.com","http://yahoo.com");
$fp = fopen('file.txt', 'w');
foreach($data as $key => $value){
fwrite($fp,$value."\t");
}
fclose($fp);
?>
Try this..
<?php
$arr=array("ABC","DEF","GHI");
$fp=fopen("test.txt","w+");
foreach($arr as $key => $value){
fwrite($fp,$value."\t");
}
?>
Try this:
file_put_contents('/path/to/file', implode("
", $urls_array));
Here are the docs: http://php.net/manual/en/function.file-put-contents.php
Try this:
<?php
$myArray = Array(0 => "http://google.com", 1 => "http://facebook.com", 2 => "http://yahoo.com");
foreach($myArray as $value){
file_put_contents("text.txt", $value."
", FILE_APPEND);
}
?>
The main benifit of file_put_contents
is that it's equivalent calling fopen()
+ fwrite()
+ fclose()
, so for simple tasks like this, it can be very useful.
You can find it's manual -> HERE.