14pulsars Posted May 17, 2008 Share Posted May 17, 2008 Hi Guys, php noobie here: I have a bit of a problem: I need to receive an indeterminate amount of similar POST variables (they are SKU's from a shopping cart). I want to place them in an array for later use and comparison with another array used to print item details based on the SKU. I tried this to place the SKU's in an array, but I get an error on the 'if' line. (unexpected T_VARIABLE, expecting ']') Obviously, there is a syntax error, but how is the following code invalid? Any suggestions? $sku = array($_POST['sku1']); for ($i = 2; $i<19; $i++) { if (isset($_POST[sku$i])) { $sku[$i-1] = $_POST[sku$i]; }else{ break; } } Link to comment https://forums.phpfreaks.com/topic/106033-multiple-post-variables-into-an-array/ Share on other sites More sharing options...
thebadbad Posted May 17, 2008 Share Posted May 17, 2008 Try this: <?php $sku = array(); for ($i = 1; $i<18; $i++) { if (isset($_POST['sku'.$i])) { $sku[$i] = $_POST['sku'.$i]; }else{ break; } } ?> Link to comment https://forums.phpfreaks.com/topic/106033-multiple-post-variables-into-an-array/#findComment-543411 Share on other sites More sharing options...
thebadbad Posted May 17, 2008 Share Posted May 17, 2008 And to make things simpler, you could consider naming the input fields with the SKU data "sku[]". Then $_POST['sku'] contains an array of values from the SKU input fields (= what you're looking for). Link to comment https://forums.phpfreaks.com/topic/106033-multiple-post-variables-into-an-array/#findComment-543412 Share on other sites More sharing options...
14pulsars Posted May 17, 2008 Author Share Posted May 17, 2008 Hey, that worked! Thanks. I didn't think about bringing in the first array addition into the loop. I also did not know you could concatenate inside variables like that. THANKS! I'm going to remember this! Try this: <?php $sku = array(); for ($i = 1; $i<18; $i++) { if (isset($_POST['sku'.$i])) { $sku[$i] = $_POST['sku'.$i]; }else{ break; } } ?> Link to comment https://forums.phpfreaks.com/topic/106033-multiple-post-variables-into-an-array/#findComment-543413 Share on other sites More sharing options...
14pulsars Posted May 17, 2008 Author Share Posted May 17, 2008 And to make things simpler, you could consider naming the input fields with the SKU data "sku[]". Then $_POST['sku'] contains an array of values from the SKU input fields (= what you're looking for). The problem is that I don't have access to those fields, the data comes from the POST from a third party CC processor. Absolutely no control over field names or anything. Though your suggestion would have been ideal. Link to comment https://forums.phpfreaks.com/topic/106033-multiple-post-variables-into-an-array/#findComment-543414 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.