Show a specific data of an associative array

1

A question about managing arrays in PHP.

I have this array:

$curso = array(
"DAW" => array(
                "1º SI" => array("horas" => 230, "plazas" => 30),
                "1º BD" => array("horas" => 200, "plazas" => 30),
                "1º PROG" => array("horas" => 260, "plazas" => 30),
                "1º ED" => array("horas" => 70, "plazas" => 30),
                "1º LMSGI" => array("horas" => 130, "plazas" => 30),
                "1º FOL" => array("horas" => 90, "plazas" => 30),

                "2º DIW" => array("horas" => 95, "plazas" => 30),
                "2º DWES" => array("horas" => 170, "plazas" => 30),
                "2º DWEC" => array("horas" => 170, "plazas" => 30),
                "2º DAW" => array("horas" => 75, "plazas" => 30),
                "2º EIE" => array("horas" => 60, "plazas" => 30),
                "2º FCT" => array("horas" => 410, "plazas" => 30),
                "2º PROYC" => array("horas" => 50, "plazas" => 30),                  
              ),

"DAM" =>array(
                "1º SI" => array("horas" => 230, "plazas" => 30),
                "1º BD" => array("horas" => 200, "plazas" => 30),
                "1º PROG" => array("horas" => 260, "plazas" => 30),
                "1º ED" => array("horas" => 70, "plazas" => 30),
                "1º LMSGI" => array("horas" => 130, "plazas" => 30),
                "1º FOL" => array("horas" => 90, "plazas" => 30),

                "2º PMD"=> array("horas"=>155, "plazas"=>30),
                "2º PSP"=> array("horas"=>155, "plazas"=>30),
                "2º SGE"=> array("horas"=>55, "plazas"=>30),
                "2º AD"=> array("horas"=>95, "plazas"=>30),
                "2º DI"=> array("horas"=>95, "plazas"=>30),
                "2º FCT"=> array("horas"=>60, "plazas"=>30),
                "2º FCT"=> array("horas"=>410, "plazas"=>30),
                "2º PROYC"=> array("horas"=>55, "plazas"=>30),

             ),

"ASIR" => array(
                "1º ISO" => array("horas" => 230, "plazas" =>30),
                "1º PAR" => array("horas" => 150, "plazas" =>30),
                "1º FH" => array("horas" => 110, "plazas" =>30),
                "1º GBD" => array("horas" => 230, "plazas" =>30),
                "1º LMSGI" => array("horas" => 140, "plazas" =>30),
                "1º FOL" => array("horas" => 90, "plazas" =>30),

                "2º ASO" => array("horas" => 135, "plazas" =>30),
                "2º SRI" => array("horas" => 170, "plazas" =>30),
                "2º IAW" => array("horas" => 170, "plazas" =>30),
                "2º ASGBD" => array("horas" => 75, "plazas" =>30),
                "2º SAD" => array("horas" => 60, "plazas" =>30),
                "2º EIE" => array("horas" => 30, "plazas" =>30),
                "2º FCT" => array("horas" => 410, "plazas" =>30),

            ));

I want to show the data only from the "ASIR" array and only from this one I want to show:

                "1º PAR" 
                "1º FH" 
                "1º GBD" 
                "1º LMSGI" 
                "1º FOL" 

I have approached doing this:

 <?php
                $mostrarModulos = "";
                foreach ($curso as $ciclos => $modulos) {

                    echo $ciclos;
                    foreach ($modulos as $modulo => $dato) {
                        $mostrarModulos .= "<label for='modulos'>'$modulo'</label><input type='checkbox' id='modulos' name='$modulo'><br>";
                        echo $mostrarModulos;
                    }
                }

                echo $curso["DAW"];
                ?>        

But of course this shows me all the modules and I just want you to show me one in particular.

Thank you.

    
asked by Carlos Rayón Álvarez 24.11.2017 в 19:40
source

2 answers

1

It's easy to simply use the foreach by passing that part of the array.

Example

foreach ($curso['ASIR'] as $key => $value) {
    echo $key.', HORAS: '.$value['horas'].' PLAZAS: '.$value['plazas'].PHP_EOL;
}

Result:

1º ISO, HORAS: 230 PLAZAS: 30
1º PAR, HORAS: 150 PLAZAS: 30
1º FH, HORAS: 110 PLAZAS: 30
1º GBD, HORAS: 230 PLAZAS: 30
1º LMSGI, HORAS: 140 PLAZAS: 30
1º FOL, HORAS: 90 PLAZAS: 30
2º ASO, HORAS: 135 PLAZAS: 30
2º SRI, HORAS: 170 PLAZAS: 30
2º IAW, HORAS: 170 PLAZAS: 30
2º ASGBD, HORAS: 75 PLAZAS: 30
2º SAD, HORAS: 60 PLAZAS: 30
2º EIE, HORAS: 30 PLAZAS: 30
2º FCT, HORAS: 410 PLAZAS: 30
    
answered by 24.11.2017 / 19:47
source
0

A solution that I found to show the last data of array was to next: (Taking into account the array that used):

foreach ($ _SESSION ['seleccion'] as $ module) {{     foreach ($ course ['DAW'] [$ module] as $ key = > $ value) {         if ($ key == 'hours') {             $ hours Cycle + = $ value;             continue; // I avoid getting the other value of the array         }     } }

    
answered by 06.12.2017 в 05:33