Reputation: 403
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()
Array 1:
Array (
[0] => Main Door
[1] => Clock
[2] => Production corridor
[3] => Warehouse Corridor
[4] => Production corridor
[5] => Warehouse Corridor
[6] => Production corridor
)
Array 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?
Upvotes: 1
Views: 72
Reputation: 596
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
Upvotes: 2
Reputation: 444
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
Upvotes: 0
Reputation: 4013
if you want results like array('Clock', '08:04:29')
:
array_combine($a1, $a2);
otherwise:
$new = array();
foreach($a1 as $k => $v) {
$new[$k] = array($v, $a2[$k]);
}
Upvotes: 2