0

I have Input fields that result in an array after POST:

<tr>
<td><input name="time['day'][]" value="1"></td>
<td><input name="time['from][]" value="1"></td>
<td><input name="time['to][]" value="1"></td>
</tr>

<tr>
<td><input name="time['day'][]" value="2"></td>
<td><input name="time['from][]" value="2"></td>
<td><input name="time['to][]" value="2"></td>
</tr>

This will be return:

Array (

['day'] => Array
    (
        [0] => 1
        [1] => 2
    )

['from] => Array
    (
        [0] => 1
        [1] => 2
    )

['to] => Array
    (
        [0] => 1
        [1] => 2
    )

)

But I would like to have this:

Array ( [1] => Array

    (
        ['day'] => 1
        ['from] => 1
        ['to] => 1
    )

[2] => Array
    (
        ['day'] => 2
        ['from] => 2
        ['to] => 2
    )

)

I get this if I use:

<tr>
<td><input name="time[1]['day']" value="1"></td>
<td><input name="time[1]['from]" value="1"></td>
<td><input name="time[1]['to]" value="1"></td>
</tr>

<tr>
<td><input name="time[2]['day']" value="2"></td>
<td><input name="time[2]['from]" value="2"></td>
<td><input name="time[2]['to]" value="2"></td>
</tr>

But here comes the problem. I want to add new rows dynamicly (with JS) and would need to add always +1 to the first index.

How could I achive the second result without having to set the first index manually?

5
  • Start with 0 and check what you have before incrementing. Commented Sep 19, 2014 at 20:03
  • Arrays are zero-indexed, you know. Commented Sep 19, 2014 at 20:03
  • "I want to add new rows dynamicly (with JS) and would need to add always +1 to the first index" - What's the problem in this? Maybe I'm just not getting it. Commented Sep 19, 2014 at 20:04
  • 1
    BTW you don't need the quotes, e.g. you can just do this: name="time[1][day]". Commented Sep 19, 2014 at 20:10
  • Two options: 1) Set the max value as a Javascript variable via PHP (messy) or 2) Fetch the value to use and increment it as needed. Here is a jQuery example. Adjust the selector as needed to match your markup: $('input').attr('name').match(/\[(\d+)\]/)[1]; Commented Sep 19, 2014 at 20:20

2 Answers 2

2

You can keep your html like this -

<tr>
    <td><input name="time['day'][]" value="1"></td>
    <td><input name="time['from'][]" value="1"></td>
    <td><input name="time['to'][]" value="1"></td>
</tr>

<tr>
    <td><input name="time['day'][]" value="2"></td>
    <td><input name="time['from'][]" value="2"></td>
    <td><input name="time['to'][]" value="2"></td>
</tr>

After you get the values on server, lets say in GET array -

<?php 
    $myfinalarray = array();
    foreach ($_GET['time'] as $key => $value) {
        foreach ($value as $k => $v) {
            $myfinalarray[$k][$key] = $v;
        }
    }
    print_r($myfinalarray);
?>

Output -

Array
(
    [0] => Array
        (
            ['day'] => 1
            ['from'] => 1
            ['to'] => 1
        )

    [1] => Array
        (
            ['day'] => 2
            ['from'] => 2
            ['to'] => 2
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

Using script to write script is better than to write each. Just a concept to go with, you can use jquery to write into div for better result.

<script>
function callInput( i ) {
  document.write( "<tr><td><input name='time[" + i + "][day]' value='" + i + "'></td><td><input name='time[" + i + "][from]' value='" + i + "'></td><td><input name='time[" + i + "][to]' value='" + i + "'></td></tr>" );
}

function callRow( i ) {
  document.write( "<table>" );
  for( a = 0; a < i; a++ ) {
    callInput( a );
  }
  document.write( "</table>" );
}
</script>

<body onload="callRow( 4 )">

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.