I have a quite nested JSON file. How can I delete an array that includes my value? For ex: I want to delete {"customer":"Customer1","date":"2017-06-03"...}
from JSON file and I already knew the "Customer1"
{
"info": [{
"customer": "Customer1",
"date": "2017-06-03",
"beacons": [{
"data1": "1234",
"data2": "Test1",
}, {
"data1": "0088",
"data2": "Test2",
}]
},{
"customer": "Customer2",
"date": "2017-06-03",
"beacons": [{
"data1": "dcdd4",
"data2": "Test3",
}, {
"data1": "0088",
"data2": "Test4",
}]
}]
}
Thanks!
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]);
}
}
make the object to assign to one obj example :=>
var myobj={"info": [{ ...... }]}
.this below function will work for the searching the exact value in object array call the function with the parameters as like below
searchObj (myobj, Customer1);
function searchObj (obj_name, searchingval) {
for (var key in obj_name) {
var value = obj_name[key];
if (typeof value === 'object') {
searchObj(value, searchingval);
}
if (value === searchingval) {
console.log('property name=' + key + ' property value=' + value);
}
}
}
one small correction in the previous answer .
please call function like this . the searching value should be a value like astring format .
searchObj (myobj, 'Customer1');