Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete element from json with php

Tags:

json

php

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):

[
{
"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:

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

like image 201
Manuel Ragazzini Avatar asked Aug 13 '13 15:08

Manuel Ragazzini


People also ask

How to remove element in json in php?

Show activity on this post. <? 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 ); ?> JSON Search and remove in php?

How to remove json object from json array in php?

"delete") { $arr_data[] = $value; } } if ($updateKey === null) { array_push($arr_data,$formdata); } else { $arr_data[$updateKey] = $formdata; } $jsondata = json_encode($arr_data); //write json data into data.

How to remove array from json array in php?

First you have to parse your json using json_decode, after that remove unused elements and on the end use json_encode to convert the array to string.

How do you remove a key value pair from a JSON object?

JsonObject::remove() removes a key-value pair from the object pointed by the JsonObject . If the JsonObject is null, this function does nothing.


1 Answers

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++;
}
like image 88
Manuel Ragazzini Avatar answered Sep 28 '22 01:09

Manuel Ragazzini