如何在PHP中将数组插入主数组以使其成为2d数组?

时间:2017-07-07 05:47:33

标签: php arrays

我有这3个阵列:

$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');

我想将这些数组放入一个将产生这种形式的数组中:

$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'));

我尝试了 array_combine array_merge ,但是他们显示了我想要产生的不同结果。请帮忙。

4 个答案:

答案 0 :(得分:4)

我不知道是否有一种更流畅的方式,但一个简单的for循环就可以解决问题:

Array
(
    [0] => Women
    [1] => children
    [2] => smile
    [3] => Abstract
    [4] => Lion
    [5] => Cheetah
)

答案 1 :(得分:2)

或者你可以这样做:

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

见这里:http://php.net/manual/de/function.array-map.php
对于简短的演示,请点击此处:http://rextester.com/VJRCA63297

答案 2 :(得分:0)

您也可以映射它们:

$result = array_map(function ($value1,$value2,$value3) {
        return [$value1,$value2,$value3];
}, $date,$time_start,$time_end);     

答案 3 :(得分:0)

你可以这样做

<?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);
?>

<强>输出:

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
        )

)