我有一个表格:
console.log(await Promise.all([sendData('data1'), sendData('data2'), sendData('data3')]));
// should logs: [ 'response1', 'response2', 'response3' ]
具有多个选择输入
<form action="index.php" method="post">
提交表单后,我得到了以下数组:
<select class="form-control" id="location" name="location[]" >
<option selected disabled value="">Choose Location</option>
<option value="closing">Closing Station</option>
<option value="device">Device/ROF</option>
<option value="merch1">General Merch 1</option>
<option value="merch2">General Merch 2</option>
</select>
<input type="hidden" name="image_name[]" value="<?php echo $file; ?>" />
我希望PHP的输出是这样的:location [0]和image [0]等: 这是我的代码:
Array (
[location] => Array (
[0] => closing
[1] => merch1
[2] => merch2 )
[image] => Array (
[0] => AL-AL.jpg
[1] => AL-AN.jpg
[2] => AL-AV.jpg
[3] => AL-CA.jpg
[4] => AL-CL.jpg
[5] => AL-CM.jpg )
[submit] => Submit
)
当前正在输出:
<?php
if (isset($_POST['submit'])){
foreach ($_POST['location'] as $location) {
foreach ($_POST['image_name'] as $image) {
echo "you have selected $image to go to this location: $location <br/>";
}
}
}
}
?>
这似乎只是一个永恒的循环。我知道有一种方法可以匹配[]值上的每个数组,然后仅在有匹配项的情况下输出,但我无法弄清楚。
答案 0 :(得分:0)
不需要2个循环,只需从位置使用键即可找到图像
<?php
$p['location'][0]='closing';
$p['location'][1]='merch1';
$p['location'][2]='merch2';
$p['image'][0]='AL-AL.jpg';
$p['image'][1]='AL-AN.jpg';
$p['image'][2]= 'AL-AV.jpg';
foreach($p['location'] as $k=>$v ){
echo "you have selected ".$p['image'][$k]." to go to this location: $v <br/>";
}
?>
输出:
您选择了AL-AL.jpg转到此位置:关闭
选择了AL-AN.jpg进入此位置:merch1
选择AL-AV.jpg转到此位置:merch2