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

时间:2015-09-22 19:46:30

标签: php arrays multidimensional-array

我需要修改一些关于多维数组的东西。 首先,我需要将包含数组的键转换为同一数组的元素。为了做到这一点,我需要找到密钥的名称。这里的问题是密钥不断改变它的名字。我将基于修改的关键字称为“grupo *”和一个数字。

这是一个数组示例(使用print_r()PHP函数提取):

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

如您所见,键[grupo *]不断更改数字,它可以是1位或2位数字。我唯一知道的是这样一个键包含一个数组。

我需要对其中一个数组做同样的事情,那就是工作日。

这是一个如何看待数组的例子(作为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"
            ]
        }
    ]
}

工作日不断变化,所以我遇到了类似的问题。

2 个答案:

答案 0 :(得分:0)

假设您的数组是:

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_keys获取其所有密钥:

$keys = array_keys($arr);
$needed_key = false;
foreach ($keys as $k) {
    if (strpos($k, 'grupo') !== false) {
        $needed_key = $k;
        break;
    }
}

if ($needed_key) {
    $arr['horario'] = $arr[$needed_key]['horario'];
    $arr['grupo'] = substr($needed_key, 5);
    unset($arr[$needed_key]);
}

答案 1 :(得分:0)

旧数组

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
                                        )

                                )

                        )

                )

        )

)

一行代码

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"
                    ]
                }
            }
        ]
    }
]

希望这会有所帮助。