2

I have text boxes with a default value. I want to put them in array output as shown below.

P.S: I need the expected array output (shown below) for my dynamic options in my online quiz program when creating questions

  if(isset($_POST['btn_submit'])
  {
    //code here
  }

<form method="post">
  //question 1 array index [0]
  <input type="text" name="option" value="1">
  <input type="text" name="option" value="2">
  <input type="text" name="option" value="3">

  //question 2 array index [1]
  <input type="text" name="option" value="1">
  <input type="text" name="option" value="2">

  <input type="submit" name="btn_submit">
</form>

EXPECTED ARRAY OUTPUT VALUES:

array ( 
    [0] => 1,2,3 
    [1] => 1,2   
)

EDIT:

It's possible to have the same name but still gets the expected array value? then, put them in one variable array

2
  • It's possible to have the same name but still gets the expected array value? u need to use different name if you want to store in different indexes. Commented Mar 18, 2016 at 6:52
  • or use like option[0][] and for second one option[1][] with one more index. Commented Mar 18, 2016 at 7:04

3 Answers 3

2

You need to use array in name attribute as:

<form method="post">
<input type="text" name="option_1[]" value="1">
<input type="text" name="option_1[]" value="2">
<input type="text" name="option_1[]" value="3">

<input type="text" name="option_2[]" value="1">
<input type="text" name="option_2[]" value="2">
</form>

PHP:

if(isset($_POST['btn_submit']))
{
    $newArr[] = implode(',',$_POST['option_1']);
    $newArr[] = implode(',',$_POST['option_2']);
    echo "<pre>";
    print_r($newArr);
}

Result:

Array
(
    [0] => 1,2,3
    [1] => 1,2
)

If you just want to use one single name option than use this:

<form method="post">
  <input type="text" name="option[0][]" value="1">
  <input type="text" name="option[0][]" value="2">
  <input type="text" name="option[0][]" value="3">

  <input type="text" name="option[1][]" value="1">
  <input type="text" name="option[1][]" value="2">

  <input type="submit" name="btn_submit">
</form>

PHP:

if(isset($_POST['btn_submit']))
{
  $newArr[] = implode(',',$_POST['option'][0]);
  $newArr[] = implode(',',$_POST['option'][1]);
  echo "<pre>";
  print_r($newArr);
}
Sign up to request clarification or add additional context in comments.

4 Comments

name="option[]" -- can i use the same name in all text boxes but stil get the expected array output?
@VanAdrianCabrera: answer udpated
i don't want to implode it. I just want to put it in one variable array then im done. how?
@VanAdrianCabrera: your expected result is: Array ( [0] => 1,2,3 [1] => 1,2 )
2

Replace your code with following code and IT WILL WORK.

 if(isset($_POST['btn_submit'])
  {
    //code here
  }

<form method="post">
  //question 1 array index [0]
  <input type="text" name="option[0][]" value="1">
  <input type="text" name="option[0][]" value="2">
  <input type="text" name="option[0][]" value="3">

  //question 2 array index [1]
  <input type="text" name="option[1][]" value="1">
  <input type="text" name="option[1][]" value="2">

  <input type="submit" name="btn_submit">
</form>

Comments

0

You can simply use [] before the option name to make it as an array.

//question 1 array index [0]
<input type="text" name="option_1[]" value="1">
<input type="text" name="option_1[]" value="2">
<input type="text" name="option_1[]" value="3">

//question 2 array index [1]
<input type="text" name="option_2[]" value="1">
<input type="text" name="option_2[]" value="2">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.