0

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.

2
  • 2
    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. Commented Mar 8, 2020 at 0:45
  • @CherryDT - You should post that as an answer and include a code example. Commented Mar 8, 2020 at 1:14

1 Answer 1

1

(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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, your explanation was very helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.