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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里