Take data from the array to independent variables. MYSQL

1

I have a query SQL that brings the data that I want perfectly. When doing a print_r it shows it to me like this:

Array ( [0] => 6 [idFormacion] => 6 ) Array ( [0] => 9 [idFormacion] => 9 ) 

Array ( [0] => 12 [idFormacion] => 12 ) Array ( [0] => 14 [idFormacion] => 14 )

Array ( [0] => 15 [idFormacion] => 15 ) Array ( [0] => 16 [idFormacion] => 16 )

What I try is to save each value of the array in independent variables.

$valor1 = 6;
$valor2 = 9;

EDIT

My query SQL :

SELECT alumnos_formacion.idFormacion 
  FROM alumnos_formacion INNER JOIN formacion
     on alumnos_formacion.idFormacion = formacion.idFormacion
WHERE alumnos_formacion.NIA=12345

EDIT 2

So far I have echoed this:

while($fila2 = $objetoBBDD->devolverFilasAssoc())
{
    foreach ($fila2 as $indice => $array)
    {
        ${'valor: '.$indice} = $array;
        echo $array;
        echo "<br>";
    }
}

What it shows me are the rows with the data I need:

6

9

12

14

15

16

What I can not get is to put each value in independent variables. For example:

$formacion1 = 6

$formacion2 = 9

etc.

    
asked by Mario Guiber 10.06.2018 в 20:25
source

1 answer

0

By the format of the array that you could sample each position of your array with a foreach referring to the key [0] that is where you are saving the value that you put in your example,:

foreach($tuarreglo as $indice => $tuarray) {
     ${"valor" . $indice } = $tuarray[0];
}

So at the end you would have a variable for each iteration. Or better you could create an arrangement where you save your values that you need:

$resultados = [];//Arreglo donde guardas los valores que te interesan
//$tuarreglo es el arreglo que contiene tu consulta
foreach($tuarreglo as $indice => $tuarray) {
        $resultados[$indice] = $tuarray[0];
    }
    
answered by 10.06.2018 в 20:58