0

I have these 3 arrays:

$date = array('2017-06-10',
              '2017-06-11',
              '2017-06-12');
$time_start = array('02:00 PM',
                    '03:00 PM',
                    '04:00 PM');
$time_end = array('05:00 PM',
                  '06:00 PM',
                  '07:00 PM');

I want to put these arrays into one array that will produce this form:

$frequency = array(array('2017-06-10','02:00 PM','05:00 PM'),
                   array('2017-06-11','03:00 PM','06:00 PM'),
                   array('2017-06-12','04:00 PM','07:00 PM'));

I tried array_combine and array_merge but they showed different results as what I wanted to produce. Please help.

MDB
  • 339
  • 4
  • 19

4 Answers4

3

I don't know if there's a slicker way, but a straightforward for loop will do the trick:

$frequency = [];

for ($i = 0; $i < sizeof($date); $i++) {
    $frequency[] = array($date[$i], $time_start[$i], $time_end[$i]);
}

print_r($frequency);

// Output:
// Array
// (
//     [0] => Array
//         (
//             [0] => 2017-06-10
//             [1] => 02:00 PM
//             [2] => 05:00 PM
//         )
//
//     [1] => Array
//         (
//             [0] => 2017-06-11
//             [1] => 03:00 PM
//             [2] => 06:00 PM
//         )
//
//     [2] => Array
//         (
//             [0] => 2017-06-12
//             [1] => 04:00 PM
//             [2] => 07:00 PM
//         )
//
// )
user94559
  • 59,196
  • 6
  • 103
  • 103
2

Or you could do:

$frequency=array_map(null,$date, $time_start, $time_end);

see here: http://php.net/manual/de/function.array-map.php
and for a short demo, here: http://rextester.com/VJRCA63297

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

You can also map them:

$result = array_map(function ($value1,$value2,$value3) {
        return [$value1,$value2,$value3];
}, $date,$time_start,$time_end);     
apokryfos
  • 38,771
  • 9
  • 70
  • 114
0

You can do like below

<?php
$complete_array = array();

$date = array('2017-06-10',
              '2017-06-11',
              '2017-06-12');
$time_start = array('02:00 PM',
                    '03:00 PM',
                    '04:00 PM');
$time_end = array('05:00 PM',
                  '06:00 PM',
                  '07:00 PM');

array_push($complete_array, $date, $time_start, $time_end);
echo "<pre>";
print_r($complete_array);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => 2017-06-10
            [1] => 2017-06-11
            [2] => 2017-06-12
        )

    [1] => Array
        (
            [0] => 02:00 PM
            [1] => 03:00 PM
            [2] => 04:00 PM
        )

    [2] => Array
        (
            [0] => 05:00 PM
            [1] => 06:00 PM
            [2] => 07:00 PM
        )

)
Binayak Das
  • 618
  • 8
  • 20