Good morning. I'm going through a foreach that runs a series of data and I want to put them in an array in a specific way.
What I have so far is this:
switch ($mes_registrado){
case "01":
$array[1] = "1er Trimestre";
$array[2] = "Enero";
break;
case "02":
$array[1] = "1er Trimestre";
$array[3] = "Febrero";
break;
case "03":
$array[1] = "1er Trimestre";
$array[4] = "Marzo";
break;
case "04":
$array[5] = "2do Trimestre";
$array[6] = "Abril";
break;
case "05":
$array[5] = "2do Trimestre";
$array[7] = "Mayo";
break;
case "06":
$array[5] = "2do Trimestre";
$array[8] = "Junio";
break;
case "07":
$array[9] = "3er Trimestre";
$array[10] = "Julio";
break;
case "08":
$array[9] = "3er Trimestre";
$array[11] = "Agosto";
break;
case "09":
$array[9] = "3er Trimestre";
$array[12] = "Septiembre";
break;
case "10":
$array[13] = "4to Trimestre";
$array[14] = "Octubre";
break;
case "11":
$array[13] = "4to Trimestre";
$array[15] = "Noviembre";
break;
case "12":
$array[13] = "4to Trimestre";
$array[16] = "Diciembre";
break;
}
As you can see, in this list of months, I would like for example, that when it was case 01, case 02 and case 03, it will add 1st Quarter in its corresponding array. And also, in each case, enter the month that corresponds to it.
In short, I want each case to do what corresponds to it and which case groups do one thing in common, if they enter these groups. All in one switch.
I had thought about making a switch inside the switch itself:
switch ($mes_registrado){
case "01":
case "02":
case "03":
$array[1] = "1er Trimestre";
switch ($mes_registrado){
case "01":
$array[2] = "Enero";
break;
case "02":
$array[3] = "Febrero";
break;
case "03":
$array[4] = "Marzo";
break;
}
break;
...
But I do not know if this is too cumbersome or sloppy, or maybe there is no other way to do it. I also thought about doing it with "if", but I see the same issue, I must do "if" nested.
UPDATED
Turning it around, I'm looking at the possibility of doing it like this:
if($mes_registrado=="01"||$mes_registrado=="02"||$mes_registrado=="03"){
$array[1] = "1er Trimestre";
switch ($mes_registrado){
case "01":
$array[2] = "Enero";
break;
case "02":
$array[3] = "Febrero";
break;
case "03":
$array[4] = "Marzo";
break;
}
} else if($mes_registrado=="04"||$mes_registrado=="05"||$mes_registrado=="06"){
$array[1] = "2do Trimestre";
switch ($mes_registrado){
case "04":
$array[2] = "Abril";
break;
case "05":
$array[3] = "Mayo";
break;
case "06":
$array[4] = "Junio";
break;
}
} ...
I do not know which method can be better. The idea is to have the least amount of code and as clear as possible (and what was more optimal, of course, he asks a lot).
Greetings