按顺序连接两个数组

时间:2022-06-11 08:18:11

I have two arrays that i need to join and then return at the end of the function. Here is what is in both arrays is the style of print_r()

我需要连接两个数组,然后在函数末尾返回。这两个数组中的样式都是print_r()

Array 1:

组1:

Array ( 
        [0] => Main Door
        [1] => Clock
        [2] => Production corridor
        [3] => Warehouse Corridor
        [4] => Production corridor
        [5] => Warehouse Corridor
        [6] => Production corridor
)

Array 2:

组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
)

So these two arrays correspond with each other so Main Door out of the first array goes with 08:04:14 out of the second array and so on, so what would be the best way to put these two arrays in to one where they are joined like that?

这两个数组相互对应第一个数组的主门在第二个数组中的主门是08:04:14等等,那么把这两个数组放到一个像这样连在一起的最好方式是什么呢?

3 个解决方案

#1


2  

if you want results like array('Clock', '08:04:29'):

如果你想要结果如数组('Clock', '08:04:29'):

array_combine($a1, $a2);

otherwise:

否则:

$new = array();
foreach($a1 as $k => $v) {
  $new[$k] = array($v, $a2[$k]);
}

#2


2  

eg:

例如:

<?php
    $array1 = array("color" => "red", 2, 4);
    $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
    $result = array_merge($array1, $array2);
    print_r($result);
?>

and go through the below link for more examples,

通过下面的链接可以看到更多的例子,

http://php.net/manual/en/function.array-merge.php

http://php.net/manual/en/function.array-merge.php

#3


0  

This should do:

这个应该做的是:

$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


Using that code you can access the data like this:

例如,使用该代码可以访问如下数据:

$desc = $a3[0][0]; // main door
$time = $a3[0][1]; // 08:04:14

#1


2  

if you want results like array('Clock', '08:04:29'):

如果你想要结果如数组('Clock', '08:04:29'):

array_combine($a1, $a2);

otherwise:

否则:

$new = array();
foreach($a1 as $k => $v) {
  $new[$k] = array($v, $a2[$k]);
}

#2


2  

eg:

例如:

<?php
    $array1 = array("color" => "red", 2, 4);
    $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
    $result = array_merge($array1, $array2);
    print_r($result);
?>

and go through the below link for more examples,

通过下面的链接可以看到更多的例子,

http://php.net/manual/en/function.array-merge.php

http://php.net/manual/en/function.array-merge.php

#3


0  

This should do:

这个应该做的是:

$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


Using that code you can access the data like this:

例如,使用该代码可以访问如下数据:

$desc = $a3[0][0]; // main door
$time = $a3[0][1]; // 08:04:14