18

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?

5 Answers 5

41

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++;
}
Sign up to request clarification or add additional context in comments.

3 Comments

The problem with this solution is that if we are working with an array of objects after this if we try a json_encode it becomes an object with several objects inside and the attribute of the main object is the key of the position it once had on the array. To fix this after the foreach we should do an $data = array_values($data) to rebase the main array.
@Murilo Thanks for pointing that out, and this should be part of the answer, but I'm not sure why the author didn't see the issue with this approach. Also answered in this question considerably further down in my search results: stackoverflow.com/a/26316131/1827734 With an in-depth explanation at stackoverflow.com/a/27088740/1827734
Along with @Murilo solution, he also need to decrement $i ($i--) in IF condition, after unset().
8

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!

Comments

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

1 Comment

I'm sorry, but your solution doesn't work (also if i changed $animals to $cars) ;)
0
foreach ($data as $key => $element) {
    $value = $element['...'];
    if (...) {
        unset($data[$key]);
        // or
        $data[$key]['...'] = '...';
    }
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.