duandan1995 2016-10-17 12:56
浏览 7
已采纳

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

I have two 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
)

I need to make a result array like this:

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

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

Ideally, there will be 4 * 2 = 8 elements in new array. I do get 8, but don't get proper data.

here is my code:

$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;
                }
            }
        }

And this is the output that I get:

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条回答 默认 最新

  • 普通网友 2016-10-17 13:14
    关注

    What about this:

    $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;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务启动失败
  • ¥15 用html创建一个个人网页,提供网页页面
  • ¥20 java项目连接SqlServer数据库报错
  • ¥15 基于面向对象的图书馆借阅管理系统
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥20 centos linux 7.9安装php8.2.18不支持mysqli模块的问题
  • ¥15 stata空间计量LM检验
  • ¥15 NAO机器人说出txt文本内容
  • ¥15 关于k8s node节点被释放后如何驱逐节点并添加新节点