我有一张表格:
<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; ?>" />
我在表单提交时有这个数组:
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中的输出是这样的: location和image等:这是我的代码:
<?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/>";
}
}
}
}
?>
这当前正在输出:
you have selected AL-AL.jpg to go to this location: closing
you have selected AL-AN.jpg to go to this location: closing
you have selected AL-AV.jpg to go to this location: closing
you have selected AL-CA.jpg to go to this location: closing
you have selected AL-CL.jpg to go to this location: closing
you have selected AL-CM.jpg to go to this location: closing
you have selected AL-AL.jpg to go to this location: merch2
you have selected AL-AN.jpg to go to this location: merch2
you have selected AL-AV.jpg to go to this location: merch2
you have selected AL-CA.jpg to go to this location: merch2
you have selected AL-CL.jpg to go to this location: merch2
you have selected AL-CM.jpg to go to this location: merch2
现在,这似乎是一个永恒的循环。我知道有一种方法可以在[]值上匹配每个数组,然后只在有匹配的地方输出,但我不能解决这个问题。
发布于 2018-08-17 07:27:01
不需要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-ALL.jpg转到此位置:关闭
您已选择AL-AN.jpg转到此位置: merch1
您已选择AL-AV.jpg转到此位置: merch2
https://stackoverflow.com/questions/51886482
复制相似问题