我有两个数组需要加入,然后在函数结束时返回。这两个数组中的内容是print_r()
数组1:
Array (
[0] => Main Door
[1] => Clock
[2] => Production corridor
[3] => Warehouse Corridor
[4] => Production corridor
[5] => Warehouse Corridor
[6] => Production corridor
)
数组2:
Array (
[0] => 08:04:14
[1] => 08:04:29
[2] => 08:07:10
[3] => 08:36:34
[4] => 08:40:40
[5] => 08:58:33
[6] => 09:00:58
)
所以这两个数组相互对应,因此第一个数组中的Main Door与第二个数组中的08:04:14
一样,依此类推,那么将这两个数组放入一个的最佳方法是什么?他们这样加入的地方?
答案 0 :(得分:2)
如果您想要array('Clock', '08:04:29')
:
array_combine($a1, $a2);
否则:
$new = array();
foreach($a1 as $k => $v) {
$new[$k] = array($v, $a2[$k]);
}
答案 1 :(得分:2)
例如:
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
并查看以下链接以获取更多示例,
答案 2 :(得分:0)
这应该做:
$a1 = array(0 => 'main door', 1 => 'clock');
$a2 = array(0 => '08:04:14', 1 => '08:04:29');
$length = count($a1);
$a3 = array();
for ($i = 0; $i < $length; ++$i) {
$a3[] = array($a1[$i], $a2[$i]);
// if you want to keep the indexes the same, use $a3[$i] instead
}
var_dump($a3);
Example
使用该代码,您可以访问如下数据:
$desc = $a3[0][0]; // main door
$time = $a3[0][1]; // 08:04:14