Once you get the array with the database information, use the array_chunk()
to divide this array by an arbitrary number (in this example it is the two).
implode()
will format each element of the array as a line in the file, because the separator is \r\n
.
To scan files in a folder on the server you can use file_put_contents()
. Another option is the combination fopen()
, fwrite()
and fclose()
.
$arr = array('url1', 'url2', 'url3', 'url4', 'url5', 'url6');
$arquivos = array_chunk($arr, 2);
$i=1;
foreach ($arquivos as $item) {
$str = implode("\r\n", $item);
file_put_contents($i++.'.txt', $str);
}
Calling array_chunk()
$arquivos
will have this structure.
Array
(
[0] => Array
(
[0] => url1
[1] => url2
)
[1] => Array
(
[0] => url3
[1] => url4
)
[2] => Array
(
[0] => url5
[1] => url6
)
)