Manuel Ragazzini
Manuel Ragazzini

Reputation: 909

Delete element from json with 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?

Upvotes: 18

Views: 70160

Answers (5)

Manuel Ragazzini
Manuel Ragazzini

Reputation: 909

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++;
}

Upvotes: 41

Aditya Yaduvanshi
Aditya Yaduvanshi

Reputation: 1

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

Upvotes: 0

Hiago Venancio
Hiago Venancio

Reputation: 81

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!

Upvotes: 8

Techie
Techie

Reputation: 45124

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

JSON Search and remove in php?

Delete from json using php

Upvotes: 3

Matěj Koub&#237;k
Matěj Koub&#237;k

Reputation: 1147

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

Upvotes: 0

Related Questions