user2710234
user2710234

Reputation: 3225

PHP echo results from an array

I have this PHP Code:

while($contacts2=mysql_fetch_array($rs))
    {
        //generate the list of emails address in an array
        $emails_list[] = $contacts2["email"];
    }

so it puts all my results into an array but when i echo $emails_list outside of the while loop it just displays Array

how can i make it display like:

result1,result2,result3 etc

Upvotes: 0

Views: 282

Answers (6)

fedorqui
fedorqui

Reputation: 289525

$emails_list is an array, so you have to loop through it to print its values:

foreach ($emails_list as $email) {
  print "email: $email";
}

Note that if you want to print an specific value, you can address with $emails_list[index]. So you can do print $emails_list[0], for example.

If you then want to print values all together, do the following:

echo "(" . implode(',',$emails_list) . ")";

Test

$a=array(1,2,3);
echo "(" . implode(',',$a) . ")";

Returns

(1,2,3)

Update

Your code in http://pastebin.com/BwWZFrzZ shows that you are using:

echo 'Email sent to:' . $emails_list . '<br/ >';

So you can print

echo 'Email sent to: (' . implode(',',$emails_list) . ')<br/ >';

Update 2

Based on the github code it comes from (https://github.com/PHPMailer/PHPMailer), you need to add one email at a time:

//this is some clever **** to do with sending emails with attachments :D
$email = new PHPMailer();

while($contacts2=mysql_fetch_array($rs))
   $email->AddAddress($contacts2["email"]);
}

$email->From      = '...';

Upvotes: 1

Ajith S
Ajith S

Reputation: 2917

Here is your answer

while($contacts2=mysql_fetch_array($rs))
{
     //generate the list of emails address in an array
     $emails_list[] = $contacts2["email"];
}

$emails_list = implode(',', $emails_list);
echo "(". $emails_list . ")";

I think you should try like this,

$mail->AddAddress('[email protected]', 'Person One');
$mail->AddAddress('[email protected]', 'Person Two');

With PHPMailer, you can do as,

while($contacts2=mysql_fetch_array($rs))
{
    $mail->AddAddress($contacts2['emails'], $contacts2['name']);
}

Upvotes: 1

A. Zalonis
A. Zalonis

Reputation: 1609

Try this code

while($contacts2=mysql_fetch_array($rs))
    {
        //generate the list of emails address in an array
        array_push($emails_list,$contacts2["email"]);
    }

To show array data you can check with a print_r (just checking if it works)

print_r($emails_list)

and you can create a loop to take data one by one like below

for(int i=0;i<count($emails_list);$i++)
{
  echo $emails_list[$i]."<br>";
}

Upvotes: 0

Guru
Guru

Reputation: 619

array_push($emails_list,$contacts2["email"]);

then print the $emails_list outside of loop may be solve your problem

Upvotes: 0

Shakti Patel
Shakti Patel

Reputation: 3862

The implode() function returns a string from the elements of an array.

 echo implode(",",$emails_list);

Upvotes: 0

Ravi Kant Mishra
Ravi Kant Mishra

Reputation: 768

try this

echo implode(',', $emails_list);

here you will find the full details php implode

Upvotes: 0

Related Questions