Skip to content
Advertisement

json with no index after unset encode array in php

I hope anyone can help me. My JSON isn’t indexed (i.e. there isn’t any key above any element)

[
{
"nome":"LABORGHINI GALLARDO",
"descrizione":"LAMBORGHINI GALLARDO ED. NERA- ANNO 2007- ",
"indirizzo_pubblicato":"autocaricateeea/LABORGHINI GALLARDO31072013-023853/LABORGHINI GALLARDO31072013-023853.json",
"indirizzo_immagine_copertina":"autocaricateeea/LABORGHINI GALLARDO31072013-023853/IMG_1414 (600x448).jpg",
"indirizzo_paginaauto":"autocaricateeea/LABORGHINI GALLARDO31072013-023853/index.html"
},
{
"nome":"RENAULT MEGANE",
"descrizione":"RENAULT MEGANE -ANNO 2006-DIESEL-CC. 1461",
"indirizzo_pubblicato":"autocaricateeea/RENAULT MEGANE31072013-024103/RENAULT MEGANE31072013-024103.json",
"indirizzo_immagine_copertina":"autocaricateeea/RENAULT MEGANE31072013-024103/P1080949 (600x450).jpg",
"indirizzo_paginaauto":"autocaricateeea/RENAULT MEGANE31072013-024103/index.html"
},
{
"nome":"FORD MONDEO",
"descrizione":"FORD MONDEO SINISTRATA- ANNO 2009- DIESEL- CC. 1997-",
"indirizzo_pubblicato":"autocaricateeea/FORD MONDEO31072013-045216/FORD MONDEO31072013-045216.json",
"indirizzo_immagine_copertina":"autocaricateeea/FORD MONDEO31072013-045216/P1080971 (600x450).jpg",
"indirizzo_paginaauto":"autocaricateeea/FORD MONDEO31072013-045216/index.html"
}
]

but after running unset() to remove an element with PHP the output JSON appears like this:

{
"1":     //   <--- **** add a key before an element
{
"nome":"Fiat Punto ",
"descrizione":"Fiat Punto Bianca",
"indirizzo_pubblicato":"autocaricateeea/Fiat Punto 14072013-042703/Fiat Punto 14072013-042703.json",
"indirizzo_immagine_copertina":"autocaricateeea/Fiat Punto 14072013-042703/P1080713 (600x450).jpg",
"indirizzo_paginaauto":"autocaricateeea/Fiat Punto 14072013-042703/index.html"
},
...........
...........
...........

How you can see there is a key before the element of JSON. I know that this behavior is caused by unset (PHP json_encode as object after PHP array unset()).

There is a way to prevent this behavior?

Advertisement

Answer

I searched and tried anyway to implement the behaviour that i searched in my question but i didn’t find nothing. I supposed that, like you can see in this answer, unset function adds indices to the array because the JSON_encode not support arrays with hole. So, to solve my problem, i load the JSON file with a jQuery function, delete the elements with jQuery and i call a ajax function to delete the files linked at the address contained in the json file:

$.getJSON('loadauto.json', function(result) {
var y =  result;
    $.each(result, function(i, field){
        if(field.indirizzo_paginaauto == x){
              delete result[i];
        }
    });
    $.ajax({
            async: true,
        cache: false,
        type: 'POST',
        url: 'deleteauto.php',
        data: { nuovofilejson: y, indirizzo: x},
        success: function(data) {
            alert ("Auto cancellata definitivamente");
        },
        error: function(data) {
            alert ("Error");
        }
    });
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement