-1

Suppose I send an array of emails. The emails is an array containing john@test.com, tom@test.com, jerry@test.com....etc

From the receiving side I am trying retrieve the emails.

   foreach ($_POST['emails'] as $i => $value) {
    echo "emails[$i] is $value<br />";
    }

Is this the correct way to do it? for the foreach loop, would it iterate until all the data out of emails array finished?

jason white
  • 687
  • 4
  • 11
  • 28
  • 2
    You probably could've tried this in less time than it took to post the question... – ceejayoz May 10 '12 at 20:50
  • I trying to send in through mobile device from the front and PHP at back.If it's not working hopefully some right pointers to make it work. – jason white May 10 '12 at 20:58

4 Answers4

1

That is correct. You can probably use a more simple loop though:

foreach ($_POST['emails'] as $email) {
    echo $email, "<br>";
}

Be sure to sanitize your inputs !


If you're calling an email-sending function or something, you can use array_walk().

function send_mail($email){
    echo "Sending email to {$email}";
}

array_walk($_POST['emails'], 'send_mail');

# Sending email to john@test.com
# Sending email to tom@test.com
# Sending email to jerry@test.com
# ...
Community
  • 1
  • 1
maček
  • 76,434
  • 37
  • 167
  • 198
0

Try

foreach ( $_POST ['emails'] as $value ) {
    echo $value , "<br />";
}
Baba
  • 94,024
  • 28
  • 166
  • 217
0

Just use $value:

foreach($_POST['emails'] as $value) {
    echo "Email: {$value}\n";
}

Or you can also use the array key to access the value, but there's no point if you don't want to edit values:

foreach($_POST['emails'] as $key => $value) {
    echo "Email: " . $_POST['emails'][$key];
}
Boby
  • 826
  • 5
  • 9
-1

No, you use that syntax for associative arrays, $_POST['emails'] is most likely, not an associative array. The correct way would be something like this instead:

foreach ($_POST['emails'] as $email) {
echo "Email: $email<br />";
}
xmc
  • 251
  • 2
  • 11