php 使用php从json中删除元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18213026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete element from json with php
提问by Manuel Ragazzini
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).
我很抱歉我的英语。我知道还有很多其他类似的问题,但我没有找到任何可以帮助我的东西(或者我可能不明白)。
I have a json like this (autocaricate.json):
我有一个这样的 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"
}
]
I want delete an element (a car, for example RENAULT MEGANE) from the json with php. I write a function like this:
我想用 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
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 文件中删除任何属性(对于我们要删除的汽车,例如 RENAULT MEGANE)?
回答by Manuel Ragazzini
After a day of work i have found the correct way to check and remove an element from JSON file.
经过一天的工作,我找到了从 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++;
}
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.
我不知道原因,但几天前我已经回答了这个问题,但它被删除了。我希望这个答案会被正确检查。我很抱歉我的英语。
回答by Hiago Venancio
It's easier do that way.
这样做更容易。
//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));
This will do Just fine. Trust me!
这会很好。相信我!
回答by Techie
<?php
$cars = json_decode($cars , true); // $cars is the json array before decoding
foreach ($cars as $key => $value) {
if (in_array('RENAULT MEGANE', $value)) {
unset($cars[$key]);
}
}
$cars = json_encode($cars );
?>
Similar questions
类似问题
回答by Matěj Koubík
foreach ($data as $key => $element) {
$value = $element['...'];
if (...) {
unset($data[$key]);
// or
$data[$key]['...'] = '...';
}
}