Jump to content

Recommended Posts

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

<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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.