Remove item from json using php

Sorry for my english. I know that there are many other similar questions, but I did not find anything that could help me (or maybe I do not 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 to remove an element (car, e.g. RENAULT MEGANE) from json with php. I am writing 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 

What code am I writing to remove any property (for the car we want to remove, for example RENAULT MEGANE) from a json file?

+6
source share
4 answers

After a working day, I found the right way to check and remove an item from a 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 do not know the reason, but I already answered the question a few days ago, but it was deleted. I hope this answer is checked correctly. Sorry for my english.

+17
source
 foreach ($data as $key => $element) { $value = $element['...']; if (...) { unset($data[$key]); // or $data[$key]['...'] = '...'; } } 
0
source
 <?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 ); ?> 

Related questions

JSON Search and delete in php?

Remove from json using php

0
source

This is easier to do.

 //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)); 

It will be just fine. Trust me!

0
source

Source: https://habr.com/ru/post/951650/


All Articles