从 JSON-Array 中完全删除 JSON-Object

Completely Delete JSON-Object from JSON-Array

如果我通过 PHP 在 JSON 数组中取消设置一些对象,那么我会得到一个带有两个空 [] 的未定义字段。但是我想删除没有 [].

的整个对象

这是我使用的代码:

// Unset Data Object from JSON-File
unset($data['server'][$Server][$ID]['id']);
unset($data['server'][$Server][$ID]['svc']);

JSON-"unset"之前的文件:

{
  "server": {
    "SERVER-01": [
        {"svc":"SVC1", "id":1},
        {"svc":"SVC2", "id":2},
        {"svc":"SVC3", "id":3},
        {"svc":"SVC4", "id":4},
        {"svc":"SVC5", "id":5}
    ],
    "SERVER-02": [
        {"svc":"SVC1", "id":1},
        {"svc":"SVC2", "id":2},
        {"svc":"SVC3", "id":3},
        {"svc":"SVC4", "id":4},
        {"svc":"SVC5", "id":5}
    ]
  }
}

JSON-"unset"之后的文件:

{
  "server": {
    "SERVER-01": [
        [],
        {"svc":"SVC2", "id":2},
        {"svc":"SVC3", "id":3},
        {"svc":"SVC4", "id":4},
        {"svc":"SVC5", "id":5}
    ],
    "SERVER-02": [
        {"svc":"SVC1", "id":1},
        {"svc":"SVC2", "id":2},
        [],
        {"svc":"SVC4", "id":4},
        {"svc":"SVC5", "id":5}
    ]
  }
}

编辑: 我得到以下输出 unset($data['server'][$Server][$ID]:

{
  "server": {
    "SERVER-01": {
        "1": {"svc":"SVC2", "id":2},
        "2": {"svc":"SVC3", "id":3},
        "3": {"svc":"SVC4", "id":4},
        "4": {"svc":"SVC5", "id":5}
    },
    "SERVER-02": [
        {"svc":"SVC1", "id":1},
        {"svc":"SVC2", "id":2},
        {"svc":"SVC3", "id":3},
        {"svc":"SVC4", "id":4},
        {"svc":"SVC5", "id":5}
    ]
  }
}

而不是这样做:

// Unset Data Object from JSON-File
unset($data['server'][$Server][$ID]['id']);
unset($data['server'][$Server][$ID]['svc']);

停在ID处,像这样:

// Unset Data Object from JSON-File
unset($data['server'][$Server][$ID];

现在当你 json_encode 你的阵列时,你会发现它完全消失了:-)