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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dwessell
Forum Commoner
Posts: 62
Joined: Fri Dec 23, 2005 2:30 pm

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

Post 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
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Code: Select all

$phoneNumber = array();
for($i=1;$i<=$numberTeam;$i++){
$phoneNumber[$i] = $_POST['phoneNumber$i'];
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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
Last edited by Zoxive on Wed Oct 25, 2006 10:47 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
dwessell
Forum Commoner
Posts: 62
Joined: Fri Dec 23, 2005 2:30 pm

Post by dwessell »

Awesome!! Thanks a ton!! Much appreciated!!
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post 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
Post Reply