1

I've been trying to wrap my head around this for the last week or so, but really need some guidance.

I need to create dynamic form fields from PHP arrays. The PHP arrays vary in size, but they are always very simple arrays.

I'd like to be able to create the # of input fields by the number of items in the array, and populate the input fields with the array data.

The array data will look like the below (2 examples).

Array
(
    [0] => 19001.WAV

    [1] => 19307.WAV

    [2] => 19002.WAV

    [3] => 19308.WAV

    [4] => 19003.WAV

    [5] => 19009.WAV

    [6] => 19004.WAV

    [7] => 19310.WAV

    [8] => 19005.WAV

    [9] => 19311.WAV

    [10] => 19009.WAV

    [11] => 19307.WAV

    [12] => 19010.WAV

    [13] => 19308.WAV

    [14] => 19013.WAV

    [15] => 19309.WAV

    [16] => 19015.WAV

)

Or:

Array
(
    [0] => 101.WAV

    [1] => 101.WAV

    [2] => 102.WAV

    [3] => 102.WAV

    [4] => 103.WAV

    [5] => 103.WAV

)

I just don't even really know what direction to go.

1

3 Answers 3

1

Please refer foreach

foreach ($arr as $value) {
    echo "<input name='data[]' value='$value'> <br />";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Sanchit. I was way overthinking this.
Not name='$value'. Maybe name='data[]'
You are right @AbraCadaver ! I was just try to keep it simple... Let me update. Thank You :)
1

If you just want to render values use foreach:

<?php 
   foreach($array as $item) {
      echo '<input type="text" name="data[]" value="'. $item .'">'
   }
?>

3 Comments

Thanks Matteo. I was just overthinking this. Its simple.
Not name='$item'. Maybe name='data[]'
@MatteoKovacic. Happy coding :)
0

Not a PHP programmer but I think it should go along the lines of:

<?php
reset($Array);
foreach ($arr as $key => $value) {
    echo "<input name='data[]' id='$key' value='$value'></input>\n";
}
?>

Hope it helps.

2 Comments

Thank you. I was just overthinking this. Its straight forward.
Need a name attribute.

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.