Forum Moderators: coopster

Message Too Old, No Replies

For loop Q

         

hermes

7:16 pm on Jan 15, 2005 (gmt 0)

10+ Year Member



I have a "for loop". In this "for loop" a variable called $reg is set.
Each iteration of the "for loop", $reg is assigned to a new value.
My question is: how I can store the values of $reg each iteration in an array?

Below is some pseudocode, which I hope further illustrates what I mean.

$count = 60;
for ($i = 1; $i <= $count; ++$i) {
$reg =
}

This has got me a bit stumped.

After I have managed to make the array.
I would then like to explode this array out into variables - each one assigned a value of $reg

I realise that this may be quite a basic Q. And i really do apoligise. I have read up on arrays and am having trouble applying it to this
situation. Thanks very much.

P.S A further complication that I would like to accommodate is that $reg may be assigned a number of values in a single "for loop". I would like to
collect all these values.
The pseudocode for this:

$count = 60;
for ($i = 1; $i <= $count; ++$i) {
$reg =
$reg =
}

If this post is difficult to follow - dont hesitate to post back and I will try my best to expand. It is just I didnt want to make this post too long and
put people off.

P.S It may be helpful to know what is actually in the "for loop":

for() {

$regex = "/(([A-Za-z0-9]+_+)?[A-Za-z0-9]+\-+)?[A-Za-z0-9]+\.+)?[A-Z
a-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)?\w+\.))*\w{1,63}\.[a-z
A-Z]{2,6}/";
preg_match_all($regex, $data, $reg))

}

dreamcatcher

7:26 pm on Jan 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can simply use:

$new_reg[] = $reg;

This creates an array with the values of $reg as it loops.

hermes

7:36 pm on Jan 15, 2005 (gmt 0)

10+ Year Member



So, do you mean it would be like:

$count = 60;
for ($i = 1; $i <= $count; ++$i) {
$new_reg[] = $reg;
}

hermes

8:44 pm on Jan 15, 2005 (gmt 0)

10+ Year Member



What is the array called in the instance:

$new_reg[] = $reg;

Is it called:

$new_reg[]

or

$reg[]

I am not quite sure. How I want to apply the name of the array:

1) I want to explode the array into variables.

$emailsss = explode(",", $new_reg[]);
print $emailsss[0];
print $emailsss[1];

2) On a different tangent.
The array is actually an array of email addresses.
I want to send an email to each email address in the array. Am I going somewhere with this code?

foreach($reg as $email){
$mailaddress = $email;
$mailsubject = "My Subject";
$mailbody = "Hello, world!";
mail($mailaddress, $mailsubject, $mailbody);
}

Many thanks for your patience

grandpa

9:59 pm on Jan 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What is the array called in the instance:

The array would be named $new_reg()
$new_reg = array();

1) I want to explode the array into variables.
$emailsss = explode(",", $new_reg[]);

Try this:
$emailsss = explode(",", $new_reg);

foreach($reg as $email){

I might do this instead:
foreach($new_reg as $key,$email) { ...

dreamcatcher

10:54 am on Jan 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the array would be $new_reg. In your example though, $reg will always have the same value as it loops.

for ($i = 1; $i <= $count; ++$i) {
$new_reg[] = $reg;
}

This would just add the value of $reg, however many times the loop executes. So if $count has the value of 5, you would have the value of $reg 5 times.

How are the values of $reg being assigned?