dsifjgogw48491752 2015-09-22 19:46
浏览 39
已采纳

在多维关联数组上查找键并修改数组结构(PHP)

I need to modify a few things about a multidimensional array. First, I need to convert a key that contains an array into an element of that same array. In order to do that, I need to find the key's name. The problem here is that the key keeps changing it's name. The key I'm gonna base the modifications on is called "grupo*" and a number.

Here's an example of the array (extracted using the print_r() PHP function):

Array
(
    [codigo] => 21201
    [nombre] => CALCULO INTEGRAL
    [creditos] => 3
    [grupo8] => Array
        (
            [horario] => Array
                (
                    [martes] => Array
                        (
                            [salon] => Array
                                (
                                    [0] => G-216
                                    [1] => G-216
                                )
                            [horaInicio] => Array
                                (
                                    [0] => 1600
                                    [1] => 1700
                                )
                            [horaFin] => Array
                                (
                                    [0] => 1700
                                    [1] => 1800
                                )
                        )
                    [jueves] => Array
                        (
                            [salon] => Array
                                (
                                    [0] => C-102
                                    [1] => C-102
                                )
                            [horaInicio] => Array
                                (
                                    [0] => 1600
                                    [1] => 1700
                                )
                            [horaFin] => Array
                                (
                                    [0] => 1700
                                    [1] => 1800
                                )
                        )
                )
        )
)
Array
(
    [codigo] => 21202
    [nombre] => FISICA MECANICA
    [creditos] => 4
    [grupo1] => Array
        (
            [horario] => Array
                (
                    [lunes] => Array
                        (
                            [salon] => Array
                                (
                                    [0] => Lab B-207
                                    [1] => Lab B-207
                                )
                            [horaInicio] => Array
                                (
                                    [0] => 1300
                                    [1] => 1400
                                )
                            [horaFin] => Array
                                (
                                    [0] => 1400
                                    [1] => 1500
                                )
                        )
                    [martes] => Array
                        (
                            [salon] => Array
                                (
                                    [0] => G-110
                                    [1] => G-110
                                )
                            [horaInicio] => Array
                                (
                                    [0] => 1300
                                    [1] => 1400
                                )
                            [horaFin] => Array
                                (
                                    [0] => 1400
                                    [1] => 1500
                                )
                        )
                    [jueves] => Array
                        (
                            [salon] => Agora 101
                            [horaInicio] => 1300
                            [horaFin] => 1400
                        )
                )
        )
)
Array
(
    [codigo] => 21203
    [nombre] => ALGEBRA LINEAL
    [creditos] => 3
    [grupo13] => Array
        (
            [horario] => Array
                (
                    [lunes] => Array
                        (
                            [salon] => B-108
                            [horaInicio] => 1100
                            [horaFin] => 1200
                        )
                    [viernes] => Array
                        (
                            [salon] => Array
                                (
                                    [0] => B-107
                                    [1] => B-107
                                )
                            [horaInicio] => Array
                                (
                                    [0] => 1000
                                    [1] => 1100
                                )
                            [horaFin] => Array
                                (
                                    [0] => 1100
                                    [1] => 1200
                                )
                        )
                )
        )
)
Array
(
    [codigo] => 21304
    [nombre] => PROGRAMACION ORIENTADA A OBJETOS
    [creditos] => 3
    [grupo4] => Array
        (
            [horario] => Array
                (
                    [miercoles] => Array
                        (
                            [salon] => Array
                                (
                                    [0] => B209 lab
                                    [1] => B209 lab
                                    [2] => B209 lab
                                )
                            [horaInicio] => Array
                                (
                                    [0] => 1400
                                    [1] => 1500
                                    [2] => 1600
                                )
                            [horaFin] => Array
                                (
                                    [0] => 1500
                                    [1] => 1600
                                    [2] => 1700
                                )
                        )
                )
        )
)
Array
(
    [codigo] => 275201
    [nombre] => MATEMATICAS DISCRETAS
    [creditos] => 2
    [grupo2] => Array
        (
            [horario] => Array
                (
                    [jueves] => Array
                        (
                            [salon] => Array
                                (
                                    [0] => A-203
                                    [1] => A-203
                                )
                            [horaInicio] => Array
                                (
                                    [0] => 1100
                                    [1] => 1200
                                )
                            [horaFin] => Array
                                (
                                    [0] => 1200
                                    [1] => 1300
                                )
                        )
                )
        )
)
Array
(
    [codigo] => MAKE
    [nombre] => MARKETING ELECTRONICO
    [creditos] => 2
    [grupo1] => Array
        (
            [horario] => Array
                (
                    [viernes] => Array
                        (
                            [salon] => Array
                                (
                                    [0] => G-219
                                    [1] => G-219
                                )
                            [horaInicio] => Array
                                (
                                    [0] => 1200
                                    [1] => 1300
                                )
                            [horaFin] => Array
                                (
                                    [0] => 1300
                                    [1] => 1400
                                )
                        )
                )
        )
)

As you can see, the key [grupo*] keeps changing number, and it can be a 1 or 2 digit number. The only thing I know is that such a key contains an array.

I need to do the same thing to one of the arrays inside, the one that is a weekday.

Here is an example of how the array is supposed to look (as JSON):

{
    "codigo": "21201",
    "nombre": "CALCULO INTEGRAL",
    "creditos": "3",
    "grupo": "8",
    "horario": [
        {
            "dia": "martes",
            "salon": [
                "G-216",
                "G-216"
            ],
            "horaInicio": [
                "1600",
                "1700"
            ],
            "horaFin": [
                "1700",
                "1800"
            ]
        },
        {
            "dia": "jueves",
            "salon": [
                "C-102",
                "C-102"
            ],
            "horaInicio": [
                "1600",
                "1700"
            ],
            "horaFin": [
                "1700",
                "1800"
            ]
        }
    ]
}

The weekday keeps changing, so I have a similar problem there.

  • 写回答

2条回答 默认 最新

  • dtcrw26206 2015-09-22 21:09
    关注

    old array

    Array
    (
        [0] => Array
            (
                [codigo] => MAKE
                [nombre] => MARKETING ELECTRONICO
                [creditos] => 2
                [grupo12] => Array
                    (
                        [horario] => Array
                            (
                                [lunes] => Array
                                    (
                                        [salon] => Array
                                            (
                                                [0] => G-219
                                                [1] => G-219
                                            )
    
                                        [horaInicio] => Array
                                            (
                                                [0] => 1200
                                                [1] => 1300
                                            )
    
                                        [horaFin] => Array
                                            (
                                                [0] => 1300
                                                [1] => 1400
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [codigo] => MAKE
                [nombre] =>  PROGRAMACION ORIENTADA A OBJETOS
                [creditos] => 3
                [grupo13] => Array
                    (
                        [horario] => Array
                            (
                                [viernes] => Array
                                    (
                                        [salon] => Array
                                            (
                                                [0] => B209 lab
                                                [1] => B209 lab
                                            )
    
                                        [horaInicio] => Array
                                            (
                                                [0] => 1400
                                                [1] => 1500
                                            )
    
                                        [horaFin] => Array
                                            (
                                                [0] => 1600
                                                [1] => 1700
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
    

    All in one line of code

    foreach ($data as $k => $groups) {
        foreach ($groups as $v => $group) {
            // find the match 
            if (preg_match('/^grupo*/', $v)) {
                // split the key into key and value.
                $key = substr($v, 0, 5);
                $value = substr($v, 5);
                // push the key into the array and assign its value 
                $data[$k][$key] = $value;
                // loop through the horario array
                foreach ($group['horario'] as $d => $dia) {
                    // push dia as key into horario array assign the key $d as value
                    $group['horario'][] = ['dia'=>$d,$dia];
                    //push the sub arrays into the new horario array 
                    //foreach ($dia as $a => $arr) {
                    //  $group['horario'][$a] = $arr;
                    //}
                    //unset the old horario array
                    unset($group['horario'][$d]);
                }
                // push the new horario in the main array
                $data[$k]['horario'] = $group['horario'];
                // unset the old grupo array
                unset($data[$k][$v]);
    
            }
        }
    }
    
    echo json_encode($data, JSON_PRETTY_PRINT);
    [
        {
            "codigo": "MAKE",
            "nombre": "MARKETING ELECTRONICO",
            "creditos": 2,
            "grupo": "12",
            "horario": [
                {
                    "dia": "lunes",
                    "0": {
                        "salon": [
                            "G-219",
                            "G-219"
                        ],
                        "horaInicio": [
                            "1200",
                            "1300"
                        ],
                        "horaFin": [
                            "1300",
                            "1400"
                        ]
                    }
                },
                {
                    "dia": "martes",
                    "0": {
                        "salon": [
                            "G-219",
                            "G-219"
                        ],
                        "horaInicio": [
                            "1200",
                            "1300"
                        ],
                        "horaFin": [
                            "1300",
                            "1400"
                        ]
                    }
                }
            ]
        }
    ]
    

    hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。