我有两个数组array1和array2。我希望将这两个数组合并为一个数组,并在下拉列表中显示合并后的数组的值。我想要第一个数组的值-第二个数组的值。
例如:
$employeePlaces1 = array(1, 2, 4,9);
$employeePlaces2 = array(3, 5, 6,7);我想在下拉列表中将值设置为$employeePlaces1[0]-$employeePlaces2[1],$employeePlaces1[0]-$employeePlaces2[1]。
1-3,
2-5,
4-6,
9-7.我该怎么做呢?
发布于 2011-02-02 18:24:03
$employee1 = array(1, 2, 4, 9);
$employee2 = array(3, 5, 6, 7);
function doMerge($n, $m) {
return $n.'-'.$m;
}
$c = array_map("doMerge", $employee1, $employee2);
print_r($c);或者使用带有lambda style functions的PHP5.3语法
$c = array_map(function($n, $m) {return $n.'-'.$m;}, $employee1, $employee2);发布于 2011-02-02 15:37:38
您可以使用array_diff函数
http://www.php.net/manual/en/function.array-diff.php
已编辑问题的答案
//assuming both the arrays have the same length
echo "<select>";
for($i=0;$i<count($employeePlaces1);$i++)
{
echo "<option>".$employeePlaces1[i]." - ".$employeePlaces2[i]."</option>";
}
echo "</select>";发布于 2011-02-02 17:05:35
下面是如何手动遍历它们并将这些值匹配在一起。
$list = array();
for($i=0; $i<=count($employeePlaces1); $i++) {
$list[] = $employeePlaces1[$i].'-'.$employeePlaces2[$i];
}还没有测试过,但应该是您需要的要点。
https://stackoverflow.com/questions/4871827
复制相似问题