json with no index after unset encode array in php

℡╲_俬逩灬. 提交于 2020-12-10 08:00:49

问题


I hope anyone can help me. My json isn't indexed (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 the unset to remove an element with php the json appear 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 from unset (PHP json_encode as object after PHP array unset()). There is a way to prevent this behavior?

Thanks in advance

MANUEL RAGAZZINI


回答1:


had the same issue until i found this solution:

In addition to the array_values technique it is possible to use array_splice and remove an element and re-index in one step:

unset($a[1]);

Instead:

array_splice($a, 1, 1);

quoted from here: https://stackoverflow.com/a/3869219/4809658




回答2:


The best way to do that is,

$json_arr = array_values($json_arr);



回答3:


Although you have solved your issue at client side, I thought it would be helpful to show my solution to this issue using PHP, the idea is to construct a new array and exclude items that are not wanted, this way you will not have the indexing issue caused by unset, something like this:

$new_nodes= array();
for ($j = 0; $j < count($composition->nodes); $j++)
{
   //exclude some nodes
   if ($id!= $composition->nodes[$j]->id)
   {
      // store only the nodes that you want:
      array_push($new_nodes, $composition->nodes[$j]);
   }
}
// and finally, use the new modified nodes array:
$composition->nodes= $new_nodes;



回答4:


Why do you need to prevent this behavior? When you convert the json to a PHP array, regardless of the JSON format is going to be an index array (have the numbers you shown) regardless.

If the numbering is incorrect use http://php.net/manual/en/function.array-values.php to fix.




回答5:


Regenerated your json with this PHP script

<?php
  $arr = array
  (array('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'),
   array('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'),
   array('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')
  );
  $arr_json = json_encode($arr);
  var_dump($arr_json);
?>

Preferably the JSON may be parsed with JS so that the required car is accessed. There is no need to modify the JSON with PHP.




回答6:


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


来源:https://stackoverflow.com/questions/18233563/json-with-no-index-after-unset-encode-array-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!