使用php从json中删除元素

18

很抱歉我的英语不好。我知道有很多类似这样的问题,但我找不到任何能够帮助我的东西(或者可能是我不理解)。

我有一个像这样的 json 文件(autocaricate.json):

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

我想用PHP从JSON中删除一个元素(例如RENAULT MEGANE这样的汽车)。我编写了以下函数:

$url = $GET['indirizzo']; //this variable get an address like autocaricateeea\/RENAULT MEGANE31072013-024103\/index.html

$file = file_get_contents('autocaricate.json');
$data = json_decode($file);
unset($file);//prevent memory leaks for large json.
//insert data here
foreach($data as $elemento) {
    $valore = $elemento['indirizzo_paginaauto'];
    if($valore == $url){
        ****** enter code here ******
    }
}
//save the file
file_put_contents('autocaricate.json',json_encode($data));
unset($data);//release memory

我该写哪段代码来从JSON文件中删除任何属性(例如RENAULT MEGANE的汽车)?

5个回答

41

我已经找到了正确的方法来检查和从JSON文件中删除元素。

$i=0;
foreach($data as $element) {
   //check the property of every element
   if($url == $element->indirizzo_paginaauto){
      unset($data[$i]);
      echo ("elemento cancellato");
   }
   $i++;
}

2
这个解决方案的问题在于,如果我们正在使用一个对象数组,那么在进行json_encode之后,它将变成一个包含多个对象的对象,并且主对象的属性是它在数组中曾经拥有的位置的键。为了解决这个问题,在foreach之后,我们应该执行$data = array_values($data)来重新设置主数组。 - Murilo
@Murilo 感谢您指出这一点,这应该是答案的一部分,但我不确定为什么作者没有看到这种方法存在的问题。此外,在我的搜索结果中,这个问题在相当远的地方得到了回答:https://stackoverflow.com/a/26316131/1827734 并且有一个深入的解释在 https://stackoverflow.com/a/27088740/1827734 - BrDaHa
除了@Murilo的解决方案之外,他还需要在IF条件中减少$i($i--),在unset()之后。 - ajaykools

8

这样做更容易。

//get all your data on file
$data = file_get_contents('teste_data.json');

// decode json to associative array
$json_arr = json_decode($data, true);

// get array index to delete
$arr_index = array();
foreach ($json_arr as $key => $value) {
    if ($value['YOUR KEY'] == SOME VALUE TO COMPARE) {
        $arr_index[] = $key;
    }
}

// delete data
foreach ($arr_index as $i) {
    unset($json_arr[$i]);
}

// rebase array
$json_arr = array_values($json_arr);

// encode array to json and save to file
file_put_contents('teste_data.json', json_encode($json_arr));

这样就可以了,相信我!


3

抱歉,但是你的解决方案不起作用(即使我将$animals更改为$cars);) - Manuel Ragazzini

0
foreach ($data as $key => $element) {
    $value = $element['...'];
    if (...) {
        unset($data[$key]);
        // or
        $data[$key]['...'] = '...';
    }
}

0
getpath = file_get_contents('file.json');
$arr = json_decode($getpath, true);
for ($i = 0; $i < count($arr); $i++) {
    if($arr[$i]['id']==$id){
        unset($arr[$i]);
        $arr = array_values($arr);

        // encode array to json and save to file
        file_put_contents('file.json', json_encode($arr));
 
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接