将两个数组映射到一个具有nxm元素的数组中

时间:2016-10-17 12:56:36

标签: php arrays

我有两个数组:

Array1
(
    [0] => Mon
    [1] => Tue
    [2] => Wed
    [3] => Thu
)
Array2
(
    [0] => 12:00 am,04:00 pm
    [1] => 07:00 am,02:00 pm
)

我需要像这样制作一个结果数组:

(
            [startTime] => Array
                (
                    [hours] => 07
                    [minutes] => 00 am
                )

            [endTime] => Array
                (
                    [hours] => 02
                    [minutes] => 00 pm
                )
            [weekday] => Monday
        )

理想情况下,新阵列中将有4 * 2 = 8个元素。我得到8,但没有得到适当的数据。

这是我的代码:

$j = 0;
        foreach($array2 as $key => $value) {
            $start_time = explode(',', $value)[0];
            if(!empty($start_time)) {
                $end_time = explode(',', $value)[1];
                $gen_timings_array[$j]['startTime']['hours'] = explode(':',$start_time)[0];
                $gen_timings_array[$j]['startTime']['minutes'] = explode(':',$start_time)[1];
                $gen_timings_array[$j]['endTime']['hours'] = explode(':',$end_time)[0];
                $gen_timings_array[$j]['endTime']['minutes'] = explode(':',$end_time)[1];

                foreach ($array1 as $key => $value) {
                    $gen_timings_array[$j]['weekday'] = $enum[$value];
                    ++$j;
                }
            }
        }

这是我得到的输出:

Array
(
    [0] => Array
        (
            [startTime] => Array
                (
                    [hours] => 12
                    [minutes] => 00 am
                )

            [endTime] => Array
                (
                    [hours] => 04
                    [minutes] => 00 pm
                )
            [weekday] => Monday
        )

    [1] => Array
        (
            [weekday] => Tuesday
        )

    [2] => Array
        (
            [weekday] => Wednesday
        )

    [3] => Array
        (
            [weekday] => Thursday
        )

    [4] => Array
        (
            [startTime] => Array
                (
                    [hours] => 07
                    [minutes] => 00 am
                )

            [endTime] => Array
                (
                    [hours] => 02
                    [minutes] => 00 pm
                )
            [weekday] => Monday
        )

    [5] => Array
        (
            [weekday] => Tuesday
        )

    [6] => Array
        (
            [weekday] => Wednesday
        )

    [7] => Array
        (
            [weekday] => Thursday
        )

)

1 个答案:

答案 0 :(得分:0)

这个怎么样:

$j = 0;
foreach($array2 as $key => $value) {
    $start_time = explode(',', $value)[0];
    if(!empty($start_time)) {
        $end_time = explode(',', $value)[1];

        foreach ($array1 as $key => $value) {
            $gen_timings_array[$j]['startTime']['hours'] = explode(':',$start_time)[0];
            $gen_timings_array[$j]['startTime']['minutes'] = explode(':',$start_time)[1];
            $gen_timings_array[$j]['endTime']['hours'] = explode(':',$end_time)[0];
            $gen_timings_array[$j]['endTime']['minutes'] = explode(':',$end_time)[1];

            $gen_timings_array[$j]['weekday'] = $enum[$value];
            ++$j;
        }
    }
}