将php多维数组从html隐藏字段传递给php

我们想将隐藏输入字段中的 php 数组从 html 传递到 php。我们如何将数组从 html 传递到 php


    <?php 

        $data = array(

         array("Volvo",22,18),

         array("BMW",15,13),

         array("Saab",5,2),

         array("Land Rover",17,15)

       );

    ?>



    <form name="excel_upload" id="excel_upload" action="" method="post">

    <input type="hidden" name="data[]"  >

    <input type="submit">

    </form>


素胚勾勒不出你
浏览 142回答 3
3回答

ibeautiful

您可以有多个具有相同名称的隐藏输入,如果名称包含括号,PHP 会将它们解析为一个数组。有关更多详细信息,请参阅此答案:HTML form with multiple hidden control elements of the same name例如:&nbsp; &nbsp; <form name="excel_upload" id="excel_upload" action="" method="post">&nbsp; &nbsp; <input type="hidden" name="data[]" value="Volvo" >&nbsp; &nbsp; <input type="hidden" name="data[]" value="BMW" >&nbsp; &nbsp; <input type="hidden" name="data[]" value="Toyota" >&nbsp; &nbsp; <input type="submit">&nbsp; &nbsp; </form>

12345678_0001

&nbsp;<?php&nbsp;&nbsp;$cars = array("Volvo", "BMW", "Toyota");&nbsp;$cars_string = implode(',',$cars);&nbsp;?><form name="excel_upload" id="excel_upload" action="" method="post">&nbsp; &nbsp; <input type="hidden" name="data" value="<?php echo $cars_string?>"&nbsp; >&nbsp; &nbsp; <input type="submit"></form>您可以使用implodePHP 的功能。要在 php 端再次解码它,请使用explodePHP 的函数

MMTTMM

你不能按原样发送数组,但你可以使用类似的东西来实现同样的事情<form name="excel_upload" id="excel_upload" action="" method="post">&nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$i = 1;&nbsp; &nbsp; &nbsp; &nbsp;foreach($cars as $car){&nbsp; &nbsp; ?>&nbsp;&nbsp;&nbsp; &nbsp; <input type="hidden" name="car<?php echo $i; ?>" value="<?php $car; ?>" >&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; <input type="submit"></form>您可以像 $_POST['car1'] 等一样在服务器端获取价值
打开App,查看更多内容
随时随地看视频慕课网APP