denoteone Posted December 10, 2009 Share Posted December 10, 2009 Am I assigning these form variables to the array the most efficient way? HTML: <form> <input type="text" name="box1" > <input type="text" name="box2" > <input type="text" name="box3" > <input type="text" name="box4" > </form> PHP: $price = array("leg_s"=>$_POST['box1'], "leg_m"=>$_POST['box2], "leg_l"=>$_POST['box3'], "leg_xl"=>$_POST['box4']); Link to comment https://forums.phpfreaks.com/topic/184692-assign-an-array/ Share on other sites More sharing options...
RussellReal Posted December 10, 2009 Share Posted December 10, 2009 <form> <input type="text" name="box[]" > <input type="text" name="box[]" > <input type="text" name="box[]" > <input type="text" name="box[]" > </form> <?php $price = array_combine(array('leg_s','leg_m','leg_1','leg_x1'),$_POST['box']); ?> Link to comment https://forums.phpfreaks.com/topic/184692-assign-an-array/#findComment-975121 Share on other sites More sharing options...
.josh Posted December 11, 2009 Share Posted December 11, 2009 ..or assign the keys in the form... <form> <input type="text" name="box[leg_s]" > <input type="text" name="box[leg_m]" > <input type="text" name="box[leg_1]" > <input type="text" name="box[leg_x1]" > </form> <?php print_r($_POST['box']); ?> Link to comment https://forums.phpfreaks.com/topic/184692-assign-an-array/#findComment-975185 Share on other sites More sharing options...
RussellReal Posted December 11, 2009 Share Posted December 11, 2009 aha! Or that I had no idea I'm coming out of the cave! Link to comment https://forums.phpfreaks.com/topic/184692-assign-an-array/#findComment-975236 Share on other sites More sharing options...
denoteone Posted December 11, 2009 Author Share Posted December 11, 2009 I like Crayon Violent but.... would I use <input type="text" name="price[leg_s]" > <input type="text" name="price[leg_m]" > <input type="text" name="price[leg_1]" > <input type="text" name="price[leg_x1]" > instead of box[leg_s] Link to comment https://forums.phpfreaks.com/topic/184692-assign-an-array/#findComment-975428 Share on other sites More sharing options...
.josh Posted December 11, 2009 Share Posted December 11, 2009 you can use whatever you want. It will end up as a 2d array regardless, where $_POST is the top level and then the name you use in the form is the 2nd level. $_POST will always start off on top. So you can do <input type="text" name="price[leg_s]" > <input type="text" name="price[leg_m]" > <input type="text" name="price[leg_1]" > <input type="text" name="price[leg_x1]" > And it is going to start off as for instance: echo $_POST['price']['leg_s']; // echoes whatever user entered into that field if you want to make it $price['leg_s'] etc... simply do $price = $_POST['price']; Link to comment https://forums.phpfreaks.com/topic/184692-assign-an-array/#findComment-975435 Share on other sites More sharing options...
denoteone Posted December 11, 2009 Author Share Posted December 11, 2009 if you want to make it $price['leg_s'] etc... simply do That's exactly what I want to do. Thanks for your help you saved me a ton of tedious work! Link to comment https://forums.phpfreaks.com/topic/184692-assign-an-array/#findComment-975439 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.