I am having a lot of trouble merging these arrays together, I have tried array_merge and array_merge_recursive but they aren't giving me the results that I want. If someone could point me in the right direction that would be amazing, I've been banging my head against the table for hours now...
Array 1:
array [
0 => array [
"start" => "04/26/2015 8:00 AM"
]
1 => array [
"start" => "04/26/2015 9:00 AM"
]
]
Array 2:
array [
0 => array [
"end" => "04/26/2015 10:00 AM"
]
1 => array [
"end" => "04/26/2015 11:00 AM"
]
]
Array 3:
array:2 [
0 => "1"
1 => "3"
]
And the result that I want, don't worry about the "2015-04-26" that is irrelevant to this question:
array [
"2015-04-26" => array [
"cities" => array [
1 => array [
"start" => "04/26/2015 8:00 AM"
"end" => "04/26/2015 10:00 AM"
]
3 => array [
"start" => "04/26/2015 9:00 AM"
"end" => "04/26/2015 11:00 AM"
]
]
]
]
In this particular case the following code will do the trick. But you should reconsider your data structure.
$arrStart = [
0 => [
"start" => "04/26/2015 8:00 AM",
],
1 => [
"start" => "04/26/2015 9:00 AM",
],
];
$arrEnd = [
0 => [
"end" => "04/26/2015 10:00 AM",
],
1 => [
"end" => "04/26/2015 11:00 AM",
],
];
$keys = [
0 => "1",
1 => "3",
];
$result = [];
foreach ($keys as $i => $key) {
$result[$key] = [
'start' => $arrStart[$i]['start'],
'end' => $arrEnd[$i]['end'],
];
}
var_dump($result);
It's just a simple for
loop that gets the values from each input array and combines them into the output array.
$result = array('2015-04-26' > array('cities' => array()));
foreach ($array1 as $id => $val1) {
$start = $val1['start'];
$end = $array2[$id]['end'];
$key = $array3[$id];
$result['2015-04-26']['cities'][$key] = array('start' => $start, 'end' => $end);
}