Jump to content

Foreach Mutiple Arrays


S1mba

Recommended Posts

Hi, I have multiple txt files and I would like to loop them all the the same time

 

Since foreach works with only one array, how do i loop and echo 3 arrays ($handle, $handle2 and $handle_date)?

 

Thanks

 

if(isset($_POST['name'])AND(isset($_POST['msg']))){
$name = $_POST['name'];
$msg = $_POST['msg'];
if(!empty($name)AND(!empty($msg))) {
$handle = fopen('1all_names.txt', 'a');
fwrite($handle, $name."\n");
fclose($handle);

$handle2 = fopen('1msg.txt', 'a');
fwrite($handle2, $msg."\n");
fclose($handle2);

$handle_date = fopen('1date.txt', 'a');
fwrite($handle_date, $date."\n");
fclose($handle_date);

Edited by S1mba
Link to comment
Share on other sites

None of those variables are arrays. I'm really not sure what you are doing as it looks like the code is simply reading the data from one file and writing it into another. Are you wanting to combine the data from the three files into a single file? If so, are the files structured in such a way that there is a one-to-one correspondence between the data in the three files?

Link to comment
Share on other sites

After a little bit of research figured out that I have forgotten to convert them to arrays

 

 

$readin_name = file('1_names.txt');

$readin_msg = file('1_msg.txt');

$readin_date = file('1_date.txt');

 

$readin_date = array_reverse($readin_date);

$readin_msg = array_reverse($readin_msg);

$readin_name = array_reverse($readin_name);

 

 

$count = count($readin_date);

 

for ($i=0; $count > $i; $i++){

echo $i.' - '.'<strong>'.$readin_name[$i].'</strong>'.' wrote '.$readin_msg[$i].' on the date '.$readin_date[$i].'<br>';

}

 

solved.

Edited by S1mba
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.