JSON Search and Remove in PHP

JSON Search and remove in php?

json_decode will turn the JSON object into a PHP structure made up of nested arrays. Then you just need to loop through them and unset the one you don't want.

<?php
$animals = '{
"0":{"kind":"mammal","name":"Pussy the Cat","weight":"12kg","age":"5"},
"1":{"kind":"mammal","name":"Roxy the Dog","weight":"25kg","age":"8"},
"2":{"kind":"fish","name":"Piranha the Fish","weight":"1kg","age":"1"},
"3":{"kind":"bird","name":"Einstein the Parrot","weight":"0.5kg","age":"4"}
}';

$animals = json_decode($animals, true);
foreach ($animals as $key => $value) {
if (in_array('Piranha the Fish', $value)) {
unset($animals[$key]);
}
}
$animals = json_encode($animals);
?>

PHP: search and remove JSON object

There is some issue in your json data. This is not valid json data; I have decoded the josn data & then check if "customer" value = 'Customer1', then remove the array from main array.

It should be like this:

$jsonData = '{"info ": [{
"customer ": "customer1 ",
"date ": "2017 - 06 - 03 ",
"beacons ": [{
"data1 ": "1234",
"data2 ": "Test1"
}]
}, {
"customer": "customer2 ",
"date": "2017 - 06 - 04 ",
"beacons": [{
"data1": "dcdd4",
"data2": "Test3"
}]
}]
}';

$myData = json_decode($jsonData,true);

foreach($myData["info"] as $k=>$arr) {
if($arr["customer"] == "customer1") {
unset($myData["info"][$k]);
}
}

How to remove object by property in JSON array using MySQL functions?

I think this JSON_SEARCH could work for you. It should return the path for you. The last parameter '$**.users' indicates to search only in paths called users

SELECT 
JSON_SEARCH(jsondata, 'one', 'a456', null, '$**.users')
FROM jsontable

Find here a Dbfiddle JSON_SEARCH

UPDATE

Combined statement with JSON_REMOVE, JSON_SEARCH and the REPLACE function which unquotes the result of JSON_SEARCH.

UPDATE jsontable
SET jsondata = JSON_REMOVE(
jsondata, REPLACE(
JSON_SEARCH( jsondata, 'one', 'a456', null, '$**.users' )
, '"'
, ''
)
);

DBFiddle with JSON_REMOVE

Additional Infos:

  • MySQL JSON_SEARCH
  • MySQL JSON_REMOVE
  • MySQL JSON PATH Syntax

php - delete by value from json object

You should

unset($json->$key->devices[$k]);

First foreach is because you have multiple objects, then you go for each devices in the currnet object and search if they have that specified name.
So you search for a value and delete by key.

Attempting to remove a json object from an array by targeting key value

Maybe you are trying to filter your $arr_data array to remove the entities when the value is "delete", Am I right? If yes, you could try this.

foreach($arr_data as $key => $value) {
if($value->value != "delete") {
$arr_data[] = $value;
}else{
unset($arr_data[$key]);
}
}

or this

$arr_data = array_filter($arr_data, function($value){
return $value->value != "delete";
});

Delete an item from json file by key

This code uses the index of the entry that matches and uses unset() from the array itself ($projectsArr['memory']). Also as this then leaves an array with missing entries (which will cause it to save as an object) it uses array_values() to reset the keys before saving the data.

foreach($projectsArr['memory'] as $key => $mydata) {
if($mydata['id'] == $selectedIitemId) {
unset($projectsArr['memory'][$key]);
$projectsArr['memory'] = array_values($projectsArr['memory']);
$save = json_encode($projectsArr,JSON_UNESCAPED_UNICODE);
file_put_contents('../db/memory.json', $save);
header('LOCATION:approve.php');
exit;
}
}

How can i delete object from json file with PHP based on ID

unset $post doesn't remove the element from the array, it just unsets that temporary variable.

After unsetting the element you need to use array_values() to get a new array with consecutive indexes. If there's a gap in the indexes, json_encode() will encode it as an object.

You also should break out of the loop once you find the element to delete and rewrite the file.

foreach ($posts as $i => $post) 
{
if ($post->id == $id)
{
unset ($posts[$i]);
$save = json_encode(array_values($posts), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
file_put_contents('posts.json', $save);
break;
}
}


Related Topics



Leave a reply



Submit