How to prevent duplicate items in a while cycle and JSON php

0

I made this code but I can not clean the second response of the while

<?php

$json = array();

$select = "SELECT id, nameVersion, urldownload 
             FROM history 
            WHERE disponibilidad ='1'  ";
$resultado = mysqli_query($mysqli, $select);
$registrot = "";
$resultadot = "";
while ($registro = mysqli_fetch_array($resultado)) {
    $i = 0;
    $reg['id'] = $registro['id'];
    $reg['nameVersion'] = $registro['nameVersion'];
    $reg['urldownload'] = $registro['urldownload'];

    $selectt = "SELECT cambio 
                  FROM changehistory, history 
                 WHERE changehistory.idhistory = history.id 
                   AND changehistory.idhistory ='{$reg['id']}';";
    $resultadot = "";
    $resultadot = mysqli_query($mysqli, $selectt);
    $registrot = "";
    while ($registrot = mysqli_fetch_array($resultadot, MYSQLI_ASSOC)) {
        $reg['cambio'][$i] = $registrot;
        $i++;
    }

    $json['history'][] = $reg;
}
mysqli_close($mysqli);
echo json_encode($json);
?>

this JSON returns to me

{
"history": [{
    "id": "1",
    "nameVersion": "0.1-Beta",
    "urldownload": "url",
    "cambio": [{
        "cambio": "ITEM 1"
    }, {
        "cambio": "ITEM 2"
    }, {
        "cambio": "ITEM 3"
    }, {
        "cambio": "ITEM 4"
    }]
}, {
    "id": "2",
    "nameVersion": "1.0",
    "urldownload": "",
    "cambio": [{
        "cambio": "ITEAM A1"
    }, {
        "cambio": "ITEM A2"
    }, {
        "cambio": "ITEM 1"
    }, {
        "cambio": "ITEM 2"
    }]
}]

}

In the id 2 only the first two items should be returned but it is not like that. I really do not know what is due, I hope you can help me. Thank you in advance.

    
asked by Mau Ü Garcia 27.12.2018 в 06:25
source

0 answers