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.
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.
Assuming your array is:
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
)
)
)
)
)
You can get all keys of it with 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]);
}