Why foreach returns only the last value from an array correctly? Solved! Thanks to Anand Bhatnagar! | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 10

Why foreach returns only the last value from an array correctly? Solved! Thanks to Anand Bhatnagar!

domains.txt contain two lines with: php.net sololearn.com So, I want ip next to every domain, but <?php $domains=file('domains.txt'); foreach($domains as $domain) { $ip = gethostbyname($domain); echo $domain. " " . $ip .'<br>'; } ?> the output is: php.net php.net sololearn.com 184.168.221.12 I need output: domain -> IP (not domain again) domain -> IP Any recommendation? Thank You SoloLearners! Maybe I have the problem how to get the domains from remote file to an array. to be continue https://www.sololearn.com/Discuss/717237/how-to-import-strings-from-domains-txt-to-an-array-in-the-index-php

15th Sep 2017, 7:55 PM
JaMi
JaMi - avatar
3 ответов
+ 4
try with a trailing dot in php.net - php.net. sololearn.com If you do a gethostbyname() and there is no trailing dot after a domainname that does not resolve, this domainname will ultimately be appended to the server-FQDN by nslookup. (SOURCE- php.net) note: may be there is some problem with your file domaina.txt because while trying this using array it works perfectly. $domains= ["php.net", "sololearners.com"];
15th Sep 2017, 1:39 PM
Anand Bhatnagar
Anand Bhatnagar - avatar
+ 9
Thank You Anand Bhatnagar!!! I think there is no problem with the domains.txt. I tried to use .,;"- behind the php.net and it is not working But yes, when I manually create an array, it is OK: $domains=['php.net', 'sololearn.com']; foreach($domains as $domain) { $ip = gethostbyname($domain); echo $domain. " " . $ip .'<br>'; } ?> That's it!!! php.net 72.52.91.14 sololearn.com 184.168.221.12 Maybe I have the problem how to get the domains from remote file to an array. You are Great Anand, Thanks again!
15th Sep 2017, 2:24 PM
JaMi
JaMi - avatar
0
my pleasure sir 😊
15th Sep 2017, 7:20 PM
Anand Bhatnagar
Anand Bhatnagar - avatar