简体   繁体   中英

How to generate a json with correct format in php?

Im tryng to generate a json with data of my database, so when I receive the data all is well, the problem happens when I put the data in JSON format it looks like that is wrong:

{"comment":[{"id_comment":1,"photo":"imgU\/default.png","full_name":"MercadoPago","comment":"Primero"}]}{"comment":[{"id_comment":2,"photo":"imgU\/default.png","full_name":"MercadoPago","comment":"Segundo"}]}

So I want to generate a json that looks like:

[{"id_comment":1,"photo":"imgU\/default.png","full_name":"MercadoPago","comment":"Primero"},
{"id_comment":2,"photo":"imgU\/default.png","full_name":"MercadoPago","comment":"Segundo"}]

My code is the next:

global $id_comentador;
global $id_comentario;
global $comment;

$arregloComentarios = json_decode($row['comentarios'], true);

foreach ($arregloComentarios as $value) {

    $id_comentario = $value["id_comentario"];
    $id_comentador = $value["id"];
    $comment = $value["comment"];

    $consultarComentador = "select id,first_name,last_name,foto from users where id='$id_comentador';";

    $coo["comment"] = array();
    $arregloProyectos = traerDatos($consultarComentador);
    $num = count($arregloProyectos);

    if ($num > 0) {

        foreach ($arregloProyectos as $row2) {

            $foto = $row2["foto"];
            $foto2 = $row2["first_name"];
            $foto3 = $row2["last_name"];
            $tmp = array();
            $tmp["id_comment"] = $id_comentario;
            $tmp["photo"] = $foto;
            $tmp["full_name"] = $foto2.''.$foto3;
            $tmp["comment"] = $comment;
            global $coo;
            $coo['comments'] = array();
            array_push($coo["comments"], $tmp);

        }//Cierra foreach

        echo json_encode($coo);
    }
}

I will be grateful for your answers.

(Copied from comment)

Create an array $comments outside of the top foreach and push into that one instead of $coo["comments"] . Then, do echo json_encode($comments) after the outside foreach .

global $id_comentador;
global $id_comentario;
global $comment;

$arregloComentarios = json_decode($row['comentarios'], true);

$comments = [];

foreach ($arregloComentarios as $value) {
    $id_comentario = $value["id_comentario"];
    $id_comentador = $value["id"];
    $comment = $value["comment"];

    $consultarComentador = "select id,first_name,last_name,foto from users where id='$id_comentador';";

    $arregloProyectos = traerDatos($consultarComentador);
    $num = count($arregloProyectos);

    if ($num > 0) {
        foreach ($arregloProyectos as $row2) {
            $foto = $row2["foto"];
            $foto2 = $row2["first_name"];
            $foto3 = $row2["last_name"];
            $tmp = array();
            $tmp["id_comment"] = $id_comentario;
            $tmp["photo"] = $foto;
            $tmp["full_name"] = $foto2.''.$foto3;
            $tmp["comment"] = $comment;

            $comments[] = $tmp;
        }//Cierra foreach
    }
}

echo json_encode($comments);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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