homepage Welcome to WebmasterWorld Guest from 54.205.95.9
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Visit PubCon.com
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
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!

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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved