Answer the question
In order to leave comments, you need to log in
How to write arrays with foreach to a file, and get it right?
Hello!
Faced the following problem:
Looping through an array:
foreach($selectedProxiesCountry as $proxy) {
file_put_contents($filename, serialize($proxy), FILE_APPEND);
}
Array
(
[ip] => 185.64.209.18
[port] => 8080
[hostname] => 185.64.209.18
[http] => 0
[https] => 1
[socks4] => 0
[socks5] => 0
[level] => 1
[yandex] => 0
[google] => 0
[mailru] => 1
[twitter] => 1
[country_code] => RU
[response] => 257
[good_count] => 5
[bad_count] => 1
[last_check] => 2020-09-04 14:48:32
[city] => Кстово
[region] => Нижегородская область
[real_ip] => 185.64.209.18
[test_time] => 0.8
[me] => 0
)
$readyProxiesArr = unserialize(file_get_contents($proxyFile));
Answer the question
In order to leave comments, you need to log in
You collect everything you need to write into one array and write it down. And so in your file, serialized arrays lie one after another in text.
If you need to filter - filter the necessary data, collect a new array and save it entirely.
file_put_contents($filename, serialize($selectedProxiesCountry));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question