0

am trying to create array with key by using html input here's my html input

<form method="post" action="">
<input type="text" name="name[][you]" value="" />
<input type="text" name="name[][he]" value="" />
<input type="text" name="name[][she]" value="" />
<input type="text" name="name[][you]" value="" />
<input type="text" name="name[][he]" value="" />
<input type="text" name="name[][she]" value="" />
<button type="submit">go</button>
</form>

my outbut is

Array ( [0] => Array ( [you] => jhon ) [1] => Array ( [he] => joy ) [2] => Array ( [she] => sarah ) [3] => Array ( [you] => samm ) [4] => Array ( [he] => petter ) [5] => Array ( [she] => susan ) )

but i want the array to be like this

Array( [0]=> array ( [you] => jhon [he] => joy [she] => sarah )[1]=> array ( [you] => pitter [he] => tom [she] => suszan ) )

is there away to do that

1
  • Why don't you write the exact numbers in the brackets - <input type="text" name="name[0][you]" value="" /> and so on? Commented May 24, 2017 at 18:59

3 Answers 3

1

try like this ==>

<form method="post" action="">
<input type="text" name="name[0][you]" value="" />
<input type="text" name="name[0][he]" value="" />
<input type="text" name="name[0][she]" value="" />
<input type="text" name="name[1][you]" value="" />
<input type="text" name="name[1][he]" value="" />
<input type="text" name="name[1][she]" value="" />
<button type="submit">go</button>
</form>

OR

<form method="post" action="">
    <?php $n = 2; // how many interval you want
    for ($i = 0; $i < $n; $i++) {
        ?>
        <input type="text" name="name[<?php echo $i; ?>][you]" value="" />
        <input type="text" name="name[<?php echo $i; ?>][he]" value="" />
        <input type="text" name="name[<?php echo $i; ?>][she]" value="" />

<?php } ?>
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

yes just like that but without set key number in the input
@MounerMostafa then try the 2nd dynamic solution given by me
0

If you want to output an array with two children then set the keys manually, name[0] for the first three inputs and name[1] for the last three.

1 Comment

so there's no way to do this without using key number in the input ?
0

Everytime you write name="[][key]" php increment automatic the key. if you write syntax like [] php increment the index of array.

Small explation For example:
If you write an array like this

$array[] = "msg1";
$array[] = "msg2";
$array[] = "msg3";

$array length will be 2 (3 elements because it starts from 0) and it is same as

$array[0] = "msg1";
$array[1] = "msg2";
$array[2] = "msg3";

This is different from above

$array[0] = "msg1";
$array[1] = "msg2";
$array[1] = "msg3";

This array will have only 1 length (2 elements only)

Solution of your question is :

<form method="post" action="">
  <input type="text" name="name[0][you]" value="" />
  <input type="text" name="name[0][he]" value="" />
  <input type="text" name="name[0][she]" value="" />
  <input type="text" name="name[1][you]" value="" />
  <input type="text" name="name[1][he]" value="" />
  <input type="text" name="name[1][she]" value="" />
  <button type="submit">go</button>
</form>

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.