Page 1 of 1

How to access variable, inside variable, in for loop :)

Posted: Wed Oct 25, 2006 10:22 pm
by dwessell
Hey all.

I have some fields being delivered via POST.. One of the fields is numberTeam, which tells me how many phoneNumber's I'll have (Another field being delivered).

So I'd like to do something like this:

for($i=1;$i<=$numberTeam;$i++){
$phoneNumber'$i' = $_POST['phoneNumber$i'];
}

But I'm not sure of the syntax for how to access that $i variable.. I know it can be done, I've seen it.. But I just can't find it now :)

Any help would be great..

Thanks
David

Posted: Wed Oct 25, 2006 10:40 pm
by Cameri

Code: Select all

$phoneNumber = array();
for($i=1;$i<=$numberTeam;$i++){
$phoneNumber[$i] = $_POST['phoneNumber$i'];
}

Posted: Wed Oct 25, 2006 10:43 pm
by s.dot
Cameri wrote:

Code: Select all

$phoneNumber = array();
for($i=1;$i<=$numberTeam;$i++){
$phoneNumber[$i] = $_POST['phoneNumber$i'];
}

Code: Select all

$phoneNumber = array();
for($i=1;$i<=$numberTeam;$i++){
$phoneNumber[$i] = $_POST['phoneNumber'.$i];
}
:P

Posted: Wed Oct 25, 2006 10:47 pm
by Zoxive
Im not quite sure that he means an array, maybe something like this..

Code: Select all

for($i=1;$i<=$numberTeam;$i++){
${'phoneNumber' . $i} = $_POST['phoneNumber' . $i];
}
-NSF

Posted: Wed Oct 25, 2006 10:47 pm
by feyd
If you have control over the naming of the fields in the form performing the submission, naming them "phoneNumber[]" will automatically generate an array in PHP.

Posted: Wed Oct 25, 2006 10:49 pm
by dwessell
Awesome!! Thanks a ton!! Much appreciated!!

Posted: Wed Oct 25, 2006 10:53 pm
by Cameri
scottayy wrote:
Cameri wrote:

Code: Select all

$phoneNumber = array();
for($i=1;$i<=$numberTeam;$i++){
$phoneNumber[$i] = $_POST['phoneNumber$i'];
}

Code: Select all

$phoneNumber = array();
for($i=1;$i<=$numberTeam;$i++){
$phoneNumber[$i] = $_POST['phoneNumber'.$i];
}
:P
8O