| Writing an unpredictable number of variables into an Array
|
adder

msg:4527033 | 5:28 pm on Dec 11, 2012 (gmt 0) | In normal circumstances if I have two variables, I can write them into an array like this
$var1 = 3; $var2 = 4; $myarray = array($var1, $var2); What if I don't know in advance how many variables I will have? Variables are sent via form input from another script with method="POST". I can get 4 variables now but a few minutes later someone else might send 14 variables. All I really need is to save the variables in an array. Could you please point me in the right direction?
|
StoutFiles

msg:4527076 | 7:50 pm on Dec 11, 2012 (gmt 0) | foreach($_POST as $name => $value) { print "$name : $value<br>"; }
|
dmorison

msg:4527347 | 1:56 pm on Dec 12, 2012 (gmt 0) | You can use [] to append to an array:
$myarray = array(); foreach($_POST as $k => $v) { $myarray[] = $v; }
|
adder

msg:4528404 | 1:04 pm on Dec 15, 2012 (gmt 0) | @dmorison, @StoutFiles great stuff, I didn't know you could do that! I realised that I can use this function and avoid defining the &_POST variables. It will automatically grab everything that is $_POSTed to this script and write into an array. Brilliant! Thank you very much!
|
|
|