当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to add an associative array as a single new item into an already existing array?

问题描述:

I have forms in my site with some questions for the users to fill.

After each form gets submitted, all the information submitted is added into an associative array.

After that I encode that array as JSON, and save it into a single field in a MySQL database.

The user may complete many forms, and I want to add every array (with the information of that single filled in form by that user), to be added to the array that's already stored in the database as a JSON string. As a new item in that array.

For example:

array 1:

array {

["examenID"]=> string(1) "4"

["cursoID"]=> string(2) "15"

["fechaExamen"]=> string(22) "04-10-2016 11:23:39 AM"

["pregunta"]=> array(2) {

[0]=> string(18) "el azul es azulado"

[1]=> string(28) "el verde es azulado también"

}

["respuestaCorrecta"]=> array(2) {

[0]=> string(1) "v"

[1]=> string(1) "f"

}

["respuestaUsuario"]=> array(2) {

[0]=> string(1) "v"

[1]=> string(1) "f"

}

["puntaje"]=> int(2)

["estado"]=> string(8) "APROBADO"

}

array 2:

array {

["examenID"]=> string(1) "6"

["cursoID"]=> string(2) "15"

["fechaExamen"]=> string(22) "03-10-2016 10:20:40 AM"

["pregunta"]=> array(2) {

[0]=> string(18) "el negro es blanco"

[1]=> string(28) "el negro es negro"

[2]=> string(28) "los colores son distintos"

}

["respuestaCorrecta"]=> array(2) {

[0]=> string(1) "f"

[1]=> string(1) "v"

[2]=> string(1) "v"

}

["respuestaUsuario"]=> array(2) {

[0]=> string(1) "f"

[1]=> string(1) "f"

[2]=> string(1) "f"

}

["puntaje"]=> int(1)

["estado"]=> string(8) "DESAPROBADO"

}

I would like to merge both of them, so it results in this:

[0] array {

["examenID"]=> string(1) "4"

["cursoID"]=> string(2) "15"

["fechaExamen"]=> string(22) "04-10-2016 11:23:39 AM"

["pregunta"]=> array(2) {

[0]=> string(18) "el azul es azulado"

[1]=> string(28) "el verde es azulado también"

}

["respuestaCorrecta"]=> array(2) {

[0]=> string(1) "v"

[1]=> string(1) "f"

}

["respuestaUsuario"]=> array(2) {

[0]=> string(1) "v"

[1]=> string(1) "f"

}

["puntaje"]=> int(2)

["estado"]=> string(8) "APROBADO"

}

[1] array {

["examenID"]=> string(1) "6"

["cursoID"]=> string(2) "15"

["fechaExamen"]=> string(22) "03-10-2016 10:20:40 AM"

["pregunta"]=> array(2) {

[0]=> string(18) "el negro es blanco"

[1]=> string(28) "el negro es negro"

[2]=> string(28) "los colores son distintos"

}

["respuestaCorrecta"]=> array(2) {

[0]=> string(1) "f"

[1]=> string(1) "v"

[2]=> string(1) "v"

}

["respuestaUsuario"]=> array(2) {

[0]=> string(1) "f"

[1]=> string(1) "f"

[2]=> string(1) "f"

}

["puntaje"]=> int(1)

["estado"]=> string(8) "DESAPROBADO"

}

I've tried this:

 $total[] = $array1;

$total[] = $array2;

But the problem is that the first time the user fills the form, it is filled as a single item. The second time it is filled as a second item, but after the third time, the new item is not stored in the expected way.

After the third time, the array gets like this, which breaks the structure:

array(2) {

[0]=> array(2) {

[0]=> array(2) { //WHY?

[0]=> NULL // WHY?

[1]=> array(8) {

["examenID"]=> string(1) "4"

["cursoID"]=> string(2) "15"

["fechaExamen"]=> string(22) "04-10-2016 11:59:48 AM"

["pregunta"]=> array(2) {

[0]=> string(18) "el azul es azulado"

[1]=> string(28) "el verde es azulado también"

}

["respuestaCorrecta"]=> array(2) {

[0]=> string(1) "v"

[1]=> string(1) "f"

}

["respuestaUsuario"]=> array(2) {

[0]=> string(1) "v"

[1]=> string(1) "v"

}

["puntaje"]=> int(1)

["estado"]=> string(8) "APROBADO"

}

}

[1]=> array(8) {

["examenID"]=> string(1) "3"

["cursoID"]=> string(2) "15"

["fechaExamen"]=> string(22) "04-10-2016 11:59:56 AM"

["pregunta"]=> array(3) {

[0]=> string(42) "El caballo blanco de San Martin era blanco"

[1]=> string(15) "La sal es dulce"

[2]=> string(19) "La lluvia es mojada"

}

["respuestaCorrecta"]=> array(3) {

[0]=> string(1) "v"

[1]=> string(1) "f"

[2]=> string(1) "v"

}

["respuestaUsuario"]=> array(3) {

[0]=> string(1) "f"

[1]=> string(1) "f"

[2]=> string(1) "f"

} ["puntaje"]=> int(1)

["estado"]=> string(11) "DESAPROBADO"

}

}

[1]=> array(8) {

["examenID"]=> string(1) "4"

["cursoID"]=> string(2) "15"

["fechaExamen"]=> string(22) "04-10-2016 12:00:03 PM"

["pregunta"]=> array(2) {

[0]=> string(18) "el azul es azulado"

[1]=> string(28) "el verde es azulado también"

}

["respuestaCorrecta"]=> array(2) {

[0]=> string(1) "v"

[1]=> string(1) "f"

}

["respuestaUsuario"]=> array(2) {

[0]=> string(1) "f"

[1]=> string(1) "f"

}

["puntaje"]=> int(1)

["estado"]=> string(8) "APROBADO"

}

}

Here is what I'm doing:

I'm querying the database, and fetching a specific result (the field "examenes"). The first time, the item fetched is a NULL field when the user didn't completed any form yet.

 $resultado = $mostrarExamen2->fetch(PDO::FETCH_ASSOC);

$respuestasAnteriores = json_decode($resultado['examenes'], true);

When the user submit the form:

if($_SERVER['REQUEST_METHOD']=='POST'){

for ($i = 0; $i < $cant; $i++) {

$ru = $_POST['respuestaUsuario'][$i];

$respuestasUsuario['examenID'] = $examenID;

$respuestasUsuario['cursoID'] = $cursoID;

$respuestasUsuario['fechaExamen'] = $fechaExamen;

$respuestasUsuario['pregunta'] = $_POST['pregunta'];

$respuestasUsuario['respuestaCorrecta'] = $_POST['rc'];

if ($ru == 'verdadero') {

$respuestasUsuario['respuestaUsuario'][] = 'v';

} else if ($ru == 'falso') {

$respuestasUsuario['respuestaUsuario'][] = 'f';

}

}

//hacemos el cálculo del examen para saber si aprobó o no

$puntaje = 0;

for ($i = 0; $i < $cant; $i++) {

$rc = $respuestasUsuario['respuestaCorrecta'][$i];

$ru = $respuestasUsuario['respuestaUsuario'][$i];

if ($rc == $ru) {

$puntaje++;

}

}

echo '<br>Examen Enviado.<br>';

echo 'Puntaje Obtenido: '.$puntaje.'<br>';

if ($puntaje >= $examen['puntajeTotal']) {

$estado = 'APROBADO';

echo 'Examen APROBADO<br>';

} else {

$estado = 'DESAPROBADO';

echo 'Examen DESAPROBADO<br>';

}

$respuestasUsuario['puntaje'] = $puntaje;

$respuestasUsuario['estado'] = $estado;

$total[] = $respuestasAnteriores;

$total[] = $respuestasUsuario;

The thing is that $respuestasAnteriores already is an array, and where it already has more than one element in it, the next one that is added up to it breaks the structure.

网友答案:
$array = array();

array_push($array, $array1, $array2);

Like this:

$array = array();

$array1 = array(
    'something' => 'somthing',
    array(
        'something' => 'somthing',
        array('something' => 'somthing'),
        array('something' => 'somthing'),
        array('something' => 'somthing')
    ),
    'something' => 'somthing'
);

$array2 = array(
    'somewhere' => 'somewhere',
    array(
        'somewhere' => 'somewhere',
        array('somewhere' => 'somewhere'),
        array('somewhere' => 'somewhere'),
        array('somewhere' => 'somewhere')
    ),
    'somewhere' => 'somewhere'
);

array_push($array, $array1, $array2);

print_r($array);

?>

Outputs:

Array
(
    [0] => Array
        (
            [something] => somthing
            [0] => Array
                (
                    [something] => somthing
                    [0] => Array
                        (
                            [something] => somthing
                        )
                    [1] => Array
                        (
                            [something] => somthing
                        )
                    [2] => Array
                        (
                            [something] => somthing
                        )
                )
        )
    [1] => Array
        (
            [somewhere] => somewhere
            [0] => Array
                (
                    [somewhere] => somewhere
                    [0] => Array
                        (
                            [somewhere] => somewhere
                        )
                    [1] => Array
                        (
                            [somewhere] => somewhere
                        )
                    [2] => Array
                        (
                            [somewhere] => somewhere
                        )
                )
        )
)

So in your case:

$total = array();
array_push($total, $respuestasAnteriores, $respuestasUsuario);
网友答案:

The only thing you need to do is:

 $total[] = {$array1, $array2}
分享给朋友:
您可能感兴趣的文章:
随机阅读: