在PHP中未设置编码数组后没有索引的json [英] json with no index after unset encode array in php

查看:82
本文介绍了在PHP中未设置编码数组后没有索引的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能帮助我.我的JSON没有索引(即,任何元素上方都没有键)

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"
}
]

,但是运行unset()用PHP删除元素后,输出JSON如下所示:

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"
},
...........
...........
...........

如何查看JSON元素之前有一个密钥. 我知道此行为是由unset( PHP json_encode作为对象在PHP数组unset())之后.

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?

推荐答案

我一直在搜索并尝试实现我在问题中搜索的行为,但没有发现任何问题.我认为,就像您可以在此看到答案,因为JSON_encode不支持带孔的数组,所以未设置函数会向数组添加索引.因此,要解决我的问题,我使用jQuery函数加载JSON文件,使用jQuery删除元素,然后调用ajax函数来删除在json文件中包含的地址处链接的文件:

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");
        }
    });
}

这篇关于在PHP中未设置编码数组后没有索引的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆