这是我的多点
<select style="width:250px"name="locatie[]"id="contact" data-
placeholder="contactpersoon" class="chosen-select" id="e9"multiple tabindex="4">
<option value=""></option>
<option value="United States, aaa, bbb, ccc">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Afghanistan">Afghanistan</option>
</select>
这是我的php代码
$arr = $_POST["contact"];
reset($arr);
while (list(, $value) = each($arr)) {
echo "Value: $value<br />'n";
}
当我选择所有选项时,
输出
价值:美国,aaa,bbb,ccc
价值:英国
价值:阿富汗
如何将第一个值(美国,aaa,bbb,ccc)分离为
价值:美国
值:aaa
值:bbb
值:ccc
价值:英国
价值:阿富汗
看看这里:Select标记中的Option可以携带多个值吗?
您可以尝试使用JSON数组,而不是逗号分隔的列表。
您需要首先循环遍历数组,拆分字符串并将所有值添加到新数组:
<?php
$original = $_POST['contact'];
$output = array();
reset($original);
foreach($original as $value) {
if(strpos($value, ',') === false)
$output[] = $value; // add value straight away if it doesn't need splitting
else {
// split string by comma and trim whitespace from pieces
$bits = explode(',', $value);
foreach($bits as $bit) {
$output[] = trim($bit);
}
}
}
// now loop through $output array to get desired result:
foreach($output as $value) {
echo "Value: " . $value . "<br>'n";
}
?>
我不太确定我是否遵循了您想要和需要的内容,但要将第一个值分离为单独的项目,并按照演示列表的顺序列出它们,您应该拆分和修剪代表第一个值的字符串。。。而在while循环中。。。
要用字符串分割字符串,可以在PHP 中查看函数爆炸
http://us3.php.net/manual/en/function.explode.php
要修剪,请查看此
http://php.net/manual/en/function.trim.php
您的字符串"United States,aaa,bbb,ccc"必须用逗号字符"分割,然后必须修剪分割数组中的元素以去除它们前面的空白。就代码而言,我将为您提供一种伪代码,您可以根据需要进行调整。
$arr = $_POST["contact"];
reset($arr);
while (list(, $value) = each($arr)) {
// when you split the string you get an array and if the number of elements in an array is more then 1 that means your string contains a comma.. You split a string into subvalues and echo them in a loop...
if (count(explode(",", $value)) > 1) {
$subvalues = explode(",", $value);
foreach ($subvalue in $subvalues){
echo "Value: trim($subvalue)<br />'n";
}
} else {
echo "Value: $value<br />'n";
}
}