0

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',
    ...
3
  • 2
    impossible indexes have to be unique. Commented Oct 6, 2016 at 20:52
  • just add your keys to the original array Commented Oct 6, 2016 at 20:52
  • So I assumed from $result[][$keys[$i]] that you wanted multidimensional, if not then see @RobertParham comment. Commented Oct 6, 2016 at 20:56

4 Answers 4

3

You can chunk it into pairs and then combine the value pairs with the key pair:

foreach(array_chunk($alldatesandtimes, 2) as $pair) {
    $result[] = array_combine(['datein', 'dateout'], $pair);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Not nearly as fancy as abra's answer but...

$newDates = array();
$dateHolder = array();
foreach($alldatesandtimes as $k=>$v){
    $key = $k%2===0 ? "dateIn" : "dateOut";
    $newDates[$key] = $v;
    if(count($newDates)===2){
        $dateHolder[] = $newDates;
        $newDates = array();
    }
}
$alldatesandtimes = $dateHolder;

https://3v4l.org/BCoYd

Comments

0

You can loop the array with incrementing the $i variable by two ( $i+2). This way you all get every second element. after that assign a new array to another array as follows

$result[]=array("datein"=>$arr[$i], "dateout" => $arr[$i+1]);

3 Comments

if you do it like that assuming $i is only incremented by one each loop, the enddate will always be the startdate for the next array.
I explicitly mentioned that the loop must increment $i+2 instead of $i++
0

Additional approaches:

  1. Use a functional-style script to chunk then make associative arrays from each chunk. Demo

    var_export(
        array_map(
            fn($pair) => array_combine(['datein', 'dateout'], $pair),
            array_chunk($alldatesandtimes, 2)
        )
    );
    
  2. Make a single pass over the input and use bit-wise operations to determine which element should receive certain data.

    $i >> 1 effectively divides by 2 and removes any decimals.
    $i & 1 determines if the variable is odd.

    Code: Demo

    $keys = ['datein', 'dateout'];
    $result = [];
    foreach ($alldatesandtimes as $i => $dt) {
        $result[$i >> 1][$keys[$i & 1]] = $dt;
    }
    var_export($result);
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.