用php从json删除元素 [英] Delete element from json with php

查看:120
本文介绍了用php从json删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起我的英语. 我知道还有很多其他这样的问题,但是我没有找到任何可以帮助我的东西(或者也许我听不懂).

i'm sorry for my english. I know that there are many other questions like this but i didn't find anything that could help me (or maybe i don't understand).

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

I have a json like this (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). 我编写了这样的函数:

I want delete an element (a car, for example RENAULT MEGANE) from the json with php. I write a function like this:

$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)?

Which code do i write for remove any property (for the car that we want remove, for example RENAULT MEGANE) from the json file?

推荐答案

工作一天后,我发现了检查和删除JSON文件中元素的正确方法.

After a day of work i have found the correct way to check and remove an element from JSON file.

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

我不知道原因,但几天前我已经回答了这个问题,但是它已被删除.我希望这个答案将被正确检查.对不起我的英语.

i don't know the reason but i have already answered at the question some days ago but it was deleted. i hope this answer will be checked correctly. I'm sorry for my english.

这篇关于用php从json删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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