the json data is
[name] => stdClass Object
(
[first] => Andy
[middle] =>
[last] => James
)
[age] => 18
[lovely] => Array
(
[0] => running
[1] => coding
[2] => -
)
I need to delete [lovely][2] because that's no answer, but I write this code, it's not working...
$keysToRemove = array("N/A", "-", "");
// Recursively iterate through object and remove keys with specified values
function cleanObject($obj) {
foreach ($obj as $key => $value) {
if (is_object($value) || is_array($value)) {
$obj->$key = cleanObject($value);
} else {
if (in_array($value, $GLOBALS['keysToRemove'])) {
unset($obj->$key);
}
}
}
//print_r($obj);
return $obj;
}
the output is
{"name":
{"first":"Andy","last":"James"},"age":18,
"lovely":["running","coding","-"]}
it's error.
the correct output is
{"name":{"first":"Andy","last":"James"},"age":18,
"lovely":["running","coding"]}
how can delete "-"??