Array in hidden text values

Greetings! I have here code that send the array value to other form.


<p><input type = 'hidden' name = 'Lname[]' value = '<?php echo $Lname;?>'></p>

<y problem is when I tried to echo my variable it shows only its single value of my array. Is there any way to get rid of these.


$Lname = $_POST['Lname'];
foreach($Lname as $L_val => $Lname){
    echo $Lname.'<br/>';
   }

Thank you.

your question is not clear. please elaborate on what you want the result to be and then show us the results of your current var_dump($_POST)

If PHP needs to give a javascript an array the best approach is to use json_encode


<script type="text/javascript">
var MyArray = <?= json_encode( $MyArray ) ?>;
</script>

HTML input elements cannot hold more than one value. If you do need to encode an array into a form for some reason you’ll have to create an input element for each value as follows.


<?php foreach ( $array as $key => $value ): ?>
<input type="hidden" name="myarray[<?= $key ?>]" value="<?= $value ?>" >
<?php endforeach ?>

^ This, however. The OP’s input is hidden, so it would suggest to me they are looking for a way to move an array of variables from one place to another (script wise) along with the main form, for which I would probably suggest using a session to achieve.

Using session certainly achieves this within the PHP pages themselves. Since the OP hasn’t clarified what they need to do it’s hard to know what answer is best. But as session is such an obvious solution I posted with the assumption that PHP needs to share something with javascript. And who knows, the page might be building a form that will be submitted to a server not under the programmer’s control for some reason.

True, I just read the op and assumed they were just trying to pass a variable from one script to another either with or along with a form.

But maybe I have came to that conclusion hastily.

I used this code but an error occurred.

<?php foreach ( $array as $key => $value ): ?>
<input type=“hidden” name=“myarray[<?= $key ?>]” value=“<?= $value ?>” >
<?php endforeach ?>

Warning: Invalid argument supplied for foreach() in C:\