简体   繁体   English

如何将关联数组作为单个新项目添加到现有数组中?

[英]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. 之后,我将该数组编码为JSON,并将其保存到MySQL数据库的单个字段中。

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. 用户可能会完成许多表单,我想添加每个数组(该用户填写表单的信息),以JSON字符串的形式添加到已经存储在数据库中的数组中。 As a new item in that array. 作为该数组中的新项目。

For example: 例如:

array 1: 阵列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: 阵列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"). 我正在查询数据库,并获取特定结果(字段“ examenes”)。 The first time, the item fetched is a NULL field when the user didn't completed any form yet. 第一次,当用户尚未填写任何表格时,获取的项目为NULL字段。

  $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. 问题是$respuestasAnteriores已经是一个数组,并且在其中已经有多个元素的地方,加起来的下一个元素破坏了结构。

$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);

您唯一需要做的是:

 $total[] = {$array1, $array2}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM