traversing multidimensional associative array in PHP with any cycle

0

// They could help me with this arrangement that does not want to print the 'period and career'

<?php
    $datos = array(
                        "periodo"   => 'Sep-Dic2018',
                        "carrera"   => 'ITIC',
                        "matricula" => array('18005xyz','18005abc','18005opq'),                    
                        "grupo"     => array('7A','7A','10A'),                           
                        "materia"   => array("PROGRAMACIÓN DE APLICACIONES","PROGRAMACIÓN DE APLICACIONES","TÓPICOS SELECTOS DE TI"),
                        "calif"     => array(10.00,9.00,8.50)
                        );

    $periodo = $datos['periodo'];
    $carrera = $datos['carrera'];
    $matricula = $datos['matricula'];
    $grupo = $datos['grupo'];
    $materia = $datos['materia'];
    $calificacion = $datos['calif'];


    foreach ($datos as $alumno => $valor) {
    echo "El almuno tiene $alumno: ".$valor[0]."</br>";
    echo "El almuno tiene $alumno: ".$valor[1]."</br>";
    echo "El almuno tiene $alumno: ".$valor[2]."</br>";
    }
    //y me tiene que salir a este similar en pantalla puede ser con cualquier ciclo, nadamas que yo lo hice con foreach pero no me sale....
    /*$alumno[0] = array( "matricula" =>'18005xyz', "grupo"=>'7A', "materia"=>"PROGRAMACIÓN DE APLICACIONES", "calif"=>10.00, "periodo" =>'Sep-Dic2018', "carrera"=>'ITIC');
    $alumno[1] = array( "matricula" =>'18005abc', "grupo"=>'7A', "materia"=>"PROGRAMACIÓN DE APLICACIONES", "calif"=>9.00, "periodo" =>'Sep-Dic2018', "carrera"=>'ITIC'); 
    $alumno[2] = array( "matricula" =>'18005opq', "grupo"=>'10A',"materia"=>"TÓPICOS SELECTOS DE TI",       "calif"=>8.50, "periodo" =>'Sep-Dic2018', "carrera" =>'ITIC');*/
    ?>
    
asked by Jesus Antonio 24.10.2018 в 03:15
source

1 answer

0

Period and career, within array asociativo , are strings. Different from the rest of the elements that are arrays .

array(
    "periodo"   => 'Sep-Dic2018',   
    "carrera"   => 'ITIC',
    ...
)

When a foreach($array as $clave => $valor) is made, for these two cases, $clave will take the values "periodo" and "carrera" , and $valor will take the values string 'Sep-Dic2018' e 'ITIC' .

For this movement $valor[0] has no meaning for these two cases. To print the corresponding values you have to refer to them as $valor directly.

For the other elements, which are arrays , you do need the numeric indexes to refer to the individual elements of those arrays ( $valor[i] ).

array(
    ...
    "matricula" => array('18005xyz','18005abc','18005opq'),                    
    "grupo"     => array('7A','7A','10A'),                           
    "materia"   => array("PROGRAMACIÓN DE APLICACIONES","PROGRAMACIÓN DE APLICACIONES","TÓPICOS SELECTOS DE TI"),
    "calif"     => array(10.00,9.00,8.50)
);

To know if an element is an array or you can not use the function is_array($valor) that returns true if the $valor is an array.

With this function you can do different things depending on the type of element you are with.

For example we can do something like this to print all the elements of $datos :

function print_array($array){
    foreach($array as $key=>$value){
        if(is_array($value)){
            echo $key . ': ' . '<br>';
            print_array($value);
        }else{
            echo $key . ': ' . $value . '<br>';
        }
    }
}

print_array($datos);
    
answered by 24.10.2018 в 04:38