Rhezashan Rhezashan - 4 months ago 9
PHP Question

Problems with the foreach output format

I am having a problem with the

foreach
php
output. The last code I wrote is :

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);
}


The output from the code above are shown like this:

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


how I can get the result like :

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>


I also tried it with
notepad ++
for editing results.txt but my knowledge is less.

I appreciate for the help

Answer

try this:

<?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);