我遇到background-color:#555; height:15px;
foreach
输出问题。我写的最后一个代码是:
php
上面代码的输出如下所示:
foreach (xxx as xx) {
$output1 = var_export($url, true);
$output2 = var_export($sername, true);
$output3 = var_export($alt, true);
$output4 = var_export($img, true);
$file = $output1.";".$output2.";".$output3.";".$output4;
file_put_contents('results.txt', $file, FILE_APPEND | LOCK_EX);
}
我如何得到如下结果:
http://www.example1.com;username1;alt1;http://image1.com
http://www.example2.com;username2;alt2;http://image2.com
http://www.example3.com;username3;alt3;http://image3.com
http://www.example4.com;username4;alt4;http://image4.com
http://www.example5.com;username5;alt5;http://image5.com
我也尝试用http://www.example.com,http://www.example.com,http://www.example.com,http://www.example.com,http://www.example.com ;username1,username2,username3,username4,username5;alt1,alt2,alt3,alt4,alt5;http://image1.com,http://image2.com;http://image3.com,http://image4.com,http://image5.com<br>
来编辑results.txt,但我的知识却少了。
我感谢您的帮助
答案 0 :(得分:0)
$output1=$output2=$output3=$output4='';
foreach (xxx as xx) {
$output1 .= var_export($url, true);
$output2 .= var_export($sername, true);
$output3 .= var_export($alt, true);
$output4 .= var_export($img, true);
}
$file = $output1.";".$output2.";".$output3.";".$output4;
file_put_contents('results.txt', $file, FILE_APPEND | LOCK_EX);
答案 1 :(得分:0)
试试这个:
<?php
$urls = $usernames = $alts = $imgs = array();
foreach (xxx as xx) {
...
...
$urls[] = $url;
$usernames[] = $username;
$alts[] = $alt;
$imgs[] = $img;
}
$file = implode(',', $urls) . ';' . implode(',', $usernames) . ';' . implode(',', $alts) . ';'. implode(',', $imgs);
file_put_contents('results.txt', $file, FILE_APPEND | LOCK_EX);