I have an array as follows:
$alldatesandtimes = [
"2016-10-04 00:00:01",
"2016-10-04 23:59:59",
"2016-10-05 00:00:01",
"2016-10-05 23:59:59",
"2016-10-06 00:00:01",
"2016-10-06 23:59:59"
]
I want to assign datein as the index of first element and dateout to 2nd element. Then datein as the index of 3rd element and dateout to 4th element and so on and so forth to all the remaining elements.
So first index shall be datein and 2nd index shall be dateout then keep on up to the last element.
I have following code:
$keys = array();
$result = array();
for ($j = 0; $j < ((sizeof($alldatesandtimes)) / 2); $j++)
{
$keys[] = "datein";
$keys[] = "dateout";
}
for ($i = 0; $i < sizeof($alldatesandtimes); $i++)
{
$result[][$keys[$i]] = $alldatesandtimes[$i];
}
it returns the following array:
$result = Array (
[0] => Array (
[datein] => 2016-10-04 00:00:01
)
[1] => Array (
[dateout] => 2016-10-04 23:59:59
)
[2] => Array (
[datein] => 2016-10-05 00:00:01
)
[3] => Array (
[dateout] => 2016-10-05 23:59:59
)
[4] => Array (
[datein] => 2016-10-06 00:00:01
)
[5] => Array (
[dateout] => 2016-10-06 23:59:59
)
)
but I want
$result = [
'datein' => '2016-10-04 00:00:01',
'dateout' => '2016-10-04 23:59:59',
'datein' => '2016-10-05 00:00:01',
...
$result[][$keys[$i]]that you wanted multidimensional, if not then see @RobertParham comment.