Get array values within another PHP array

3

I have two arrays which contain the same number of values each. One contains the id of materia and the other calificaciones .

I need to access the qualifications to later enter them into a database. The problem is that I can access the subjects with a cycle for . But how can I access the ratings within the same cycle?

This is my code, try another cycle for inside but do not do what I want.

<?php
//require('../../../php/cone.php');

$id = $_POST['id'];
$matricula = $_POST['matricula'];
$periodo = $_POST['periodo'];
$calificacion = $_POST['calificacion'];  //Esto es un array
$materia = $_POST['materia'];            //Esto es un array


//Recorro todos los elementos que hay en materia
for ($i=0;$i<count($materia);$i++)
      {

      //saco el valor de cada elemento
      echo "Materia: ".$materia[$i]."";

      //Aqui necesito acceder al array que esta en la variable materia.
      //Intente con otro ciclo for dentro pero no hace lo que deseo.
      echo "Calificacion: ".$calificacion."";
      echo "<br>";

}

?>
    
asked by Agustin Acosta 11.05.2016 в 18:45
source

2 answers

4

According to the details you give, both arrays have the same number of data, then, assuming that they are arranged in the corresponding order (Position 0 of matter corresponds to position 0 of qualification), it is enough to use the same index to reference the key in both arrays:

for ($i = 0; $i < count($materia); $i++) {

  echo "Materia: " . $materia[$i] . "";

  echo "Calificacion: " . $calificacion[$i] . "";

  echo "<br>";

}
    
answered by 11.05.2016 / 18:52
source
3

another way of doing it, and assuming that the key of the array of subjects also corresponds to a value in the ratings array is as follows:

foreach ($materia as $key => $value) {
  echo "Materia: " . $value . " ";
  echo "Calificación: " . $calificacion[$key] . " ";
  echo "<br>";
}

Here you would be using a structure that iterates over each element of the array, returning the key and value of each "round".

    
answered by 12.05.2016 в 14:49