Using a variable in foreach not working

Simply put: I’m trying to use a variable with [$x] inside a foreach loop. Take a look at this code (it’s not my full code, just the relevant part):


foreach ($addresses as $Email_to)

{

 

$x=0;

 

$msg=str_replace("-text-","$usertext[$x]","$msg");

 

$x++;

When the full script runs, $usertext[$x] always comes out as “Array.” How can I get this script to accept [$x]?

That’s telling you that the array entry for key $x is an array rather than a string (which is what it looks like you’re expecting). You’d have to look at the rest of the code to find out where the contents of $usertext are coming from.

I’m aware of this. The array works fine when I use it elsewhere.It just won’t work in the foreach () loop. I’m trying to use the var in the loop.

You’ve actually got another problem with your code - you’re assigning $x the value of 0 on every iteration of the loop, so you’ll never get past the first element of the $usertext array.

How can I increment the value? ++ isn’t working, same with $x=x+1.

Set it to 0 outside of the foreach. That way it doesn’t get set to 0 every iteration.

1 Like

I’ve went against my mantra (to never code while being tired) and confused myself. Taking $x out of the loop was needed, yeah. Thanks for pointing it out.

My full script will send an email to each addy in my userbase. The script was found online & modified by me. After 1 error gets fixed, another presents itself. Take a look at my full code (minus DB connection info, mail host info, plus I’ve renamed a var or two for clarification):

[code] <?php

$host = “XXX”;
$username = “XXX”; //SMTP username
$password = “XXX”; // SMTP password

require(“class.phpmailer.php”);
require(“mainvars.php”);

$uploaddir = ‘upload’;
$key = 0;
$tmp_name = $_FILES[“userfile”][“tmp_name”][$key];
$name = $_FILES[“userfile”][“name”][$key];
$sendfile = “$uploaddir/$name”;
move_uploaded_file($tmp_name, $sendfile);

//exit;

$url = $_POST[‘url’];
$id = $_POST[‘id’];

$their = $_POST[‘theirname’];
$their = preg_split(‘/ +/’, $their);

$to = $_POST[‘to’];
$addresses = array();

$addresses = explode(“\n”,$to);
//print_r($addresses);exit;

$name = $_POST[‘who’];
$email_subject = $_POST[‘subject’];
$Email_msg = $_POST[‘message’];
$Email_msg2 = str_replace(“\n”, “
”, $Email_msg);;

//$Email_to = “you@yourSite.com”; // the one that recieves the email
$email_from = $_POST[‘from’];
//$dir = “uploads/$filename”;
//chmod(“uploads”,0777);
$attachments = array();

//foreach ($addresses as $Email_to) { echo $Email_to.“
”; }
//exit;

$x=0;

foreach ($addresses as $Email_to)
{

$tn=$their[$x];

$Email_msg2=str_replace(“-name-”,“$tn”,“$Email_msg2”);

$mail = new PHPMailer();

$mail->IsSMTP(); // send via SMTP
$mail->Host = $host; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password

$mail->From = $email_from;
$mail->FromName = $name;
$mail->AddAddress($Email_to);
//$mail->AddReplyTo(“info@worldtradetown.com”,“Information”);
//foreach($attachments as $key => $value) { //loop the Attachments to be added …
//$mail->AddAttachment(”uploads”.”/”.$value);
//}

$mail->AddAttachment($sendfile);

$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML

$mail->Subject = $email_subject;
$mail->Body = $Email_msg2;
$mail->AltBody = $Email_msg;

if(!$mail->Send())
{
echo “Message was not sent

”;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo “Message to $Email_to has been sent
”;

$Email_msg2=str_replace(“$tn”,“-name-”,“$Email_msg2”);

$x++;
}

?>
[/code]

Sometimes, the $tn var is ‘array’ no matter what I do. Others, the script prints off the email text multiple times for each email. If I run this script without so much as trying to call $tn, everything works perfectly. The error in this code, I just cannot see.

Hey NightProwler,

What do you get if you var_dump($their) (after you call preg_split on it)?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.