Combine elements in a switch in PHP

0

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

    
asked by José González 13.10.2017 в 17:23
source

3 answers

1
$num = intval($fact_mes_registrado);
$meses = [NULL,"Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
$trimestre = [NULL, "1er", "2do", "3er"];
$arreglo = [];
$arreglo[] = $trimestre[ceil($num/3)].' Trimestre';
$arreglo[] = $meses[$num];
print_r($arreglo);

This in my opinion is the least code would occupy, but if you put it in a for each as you mentioned it can be better so

$fact_mes_array = ["03", "07", "09", "01"];
$meses = [NULL,"Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
$trimestre = [NULL, "1er", "2do", "3er", "4to"];
$arreglo = [];
foreach($fact_mes_array as $fact_mes_registrado){
    $num = intval($fact_mes_registrado);
    $arreglo[$trimestre[ceil($num/3)].' Trimestre'][] = $meses[$num];
}
print_r($arreglo);

foreach($arreglo as $trimestre => $meses){
    echo $trimestre." : ";
    foreach($meses as $mes) {
        echo $mes.', ';
    }
    echo "; \n";
}
    
answered by 13.10.2017 в 20:17
1

My attempt to make the minimum code possible, adapted from a code that I used once but with dates in English and with more calculations stuck. That's why it helps you get some idea:)

setlocale(LC_TIME, 'es_ES', 'es_ES.UTF-8', 'Spanish_Spain.1252');
$formatter = new NumberFormatter('es_ES', NumberFormatter::ORDINAL);
$trimestres = array_chunk(range(1, 12), 3);

foreach ($trimestres as $key => $trimestre) {

  $formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, '%spellout-ordinal');    
  print '<br>' . $formatter->format($key + 1) . ' trimestre<br>';

  foreach ($trimestre as $mes) {        
    print strftime('%B', mktime(0, 0, 0, $mes)) . '<br>' ;
  }

}

Use setlocale() so that date and time values are in Spanish. So I can get them with strftime() and avoid writing the names of the months by hand one by one ^ _ ^.

Use NumberFormatter() to obtain the ordinal of the quarter number. This in English is perfect, because it is 1st, 2nd, 3rd and 4th. In Spanish it is limited to adding 'º' next to the number.

With range() I get an array of numbers from 1 to 12, and with array_chunk() I divide that array into four parts of 3 elements each, that is, the quarters. Resulting in this:

Array
(
[0] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
    )

[1] => Array
    (
        [0] => 4
        [1] => 5
        [2] => 6
    )

[2] => Array
    (
        [0] => 7
        [1] => 8
        [2] => 9
    )

[3] => Array
    (
        [0] => 10
        [1] => 11
        [2] => 12
    )

)

Finally, with the foreach I go through the arrays and show the quarter number and the name of the month.

1º trimestre
enero
febrero
marzo

2º trimestre
abril
mayo
junio

3º trimestre
julio
agosto
septiembre

4º trimestre
octubre
noviembre
diciembre
    
answered by 14.10.2017 в 03:26
1

Good morning. The solutions provided seem very interesting to me and I will address them for future developments. Even in this one, in a future revision that I will soon see how I can apply it.

For the moment, I have applied this solution. At least it does what I need and apparently, well and without much burden:

$fact_meses_let_array = array("","","","","","","","","","","","","","","","","");

if($fact_mes_registrado=="01"||$fact_mes_registrado=="02"||$fact_mes_registrado=="03"){
    $fact_meses_let_array[1] = "1er Trimestre";
    switch ($fact_mes_registrado){
        case "01":
            $fact_meses_let_array[2] = "Enero";
            break;
        case "02":
            $fact_meses_let_array[3] = "Febrero";
            break;
        case "03":
            $fact_meses_let_array[4] = "Marzo";
            break;
    }
} else if($fact_mes_registrado=="04"||$fact_mes_registrado=="05"||$fact_mes_registrado=="06"){
    $fact_meses_let_array[5] = "2do Trimestre";
    switch ($fact_mes_registrado){
        case "04":
            $fact_meses_let_array[6] = "Abril";
            break;
        case "05":
            $fact_meses_let_array[7] = "Mayo";
            break;
        case "06":
            $fact_meses_let_array[8] = "Junio";
            break;
    }
} else if($fact_mes_registrado=="07"||$fact_mes_registrado=="08"||$fact_mes_registrado=="09"){
    $fact_meses_let_array[9] = "3er Trimestre";
    switch ($fact_mes_registrado){
        case "07":
            $fact_meses_let_array[10] = "Julio";
            break;
        case "08":
            $fact_meses_let_array[11] = "Agosto";
            break;
        case "09":
            $fact_meses_let_array[12] = "Septiembre";
            break;
    }
} else if($fact_mes_registrado=="10"||$fact_mes_registrado=="11"||$fact_mes_registrado=="12"){
    $fact_meses_let_array[13] = "4to Trimestre";
    switch ($fact_mes_registrado){
        case "10":
            $fact_meses_let_array[14] = "Octubre";
            break;
        case "11":
            $fact_meses_let_array[15] = "Noviembre";
            break;
        case "12":
            $fact_meses_let_array[16] = "Diciembre";
            break;
    }
}

It does not look like a code that's too clean, but it works well. Then with the array, I only print the values that have been filled in and so I add my list.

UPDATE

I have planned another strategy, which seems to me somewhat cleaner

$fact_meses_let_array = array("","","","","","","","","","","","","","","","","");
$fact_meses_nombres_array = array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");

...[AQUI HABRIA UN FOREACH]...

switch ($fact_mes_registrado){
    case "01":
    case "02":
    case "03":
        $fact_meses_let_array[1] = "1er Trimestre";
        $fact_meses_let_array[(int)$fact_mes_registrado+1] = $fact_meses_nombres_array[(int)$fact_mes_registrado-1];
        break;
    case "04":
    case "05":
    case "06":
        $fact_meses_let_array[5] = "2do Trimestre";
        $fact_meses_let_array[(int)$fact_mes_registrado+2] = $fact_meses_nombres_array[(int)$fact_mes_registrado-1];
        break;
    case "07":
    case "08":
    case "09":
        $fact_meses_let_array[9] = "3er Trimestre";
        $fact_meses_let_array[(int)$fact_mes_registrado+3] = $fact_meses_nombres_array[(int)$fact_mes_registrado-1];
        break;
    case "10":
    case "11":
    case "12":
        $fact_meses_let_array[13] = "4to Trimestre";
        $fact_meses_let_array[(int)$fact_mes_registrado+4] = $fact_meses_nombres_array[(int)$fact_mes_registrado-1];
        break;
}

At least, this seems more direct, with less code and I avoid using conditionals within other conditionals.

Thank you all for the help!

    
answered by 16.10.2017 в 10:31