homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Writing an unpredictable number of variables into an Array

 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?



 7:50 pm on Dec 11, 2012 (gmt 0)

foreach($_POST as $name => $value) {
print "$name : $value<br>";


 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;


 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!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved