dongquexi1990 2017-07-20 13:23
浏览 57
已采纳

从PHP / codeigniter中的JSON数组中删除子元素

I am getting below json array as and i want to remove the child node "n":[] - when it is empty. (PHP)

INPUT:

[
  {
    "level": 1,
    "id": "101",
    "n": [
      {
        "level": 2,
        "id": "102",
        "n": [

        ]
      }
    ]
  },
  {
    "level": 1,
    "id": "103",
    "n": [
      {
        "level": 2,
        "id": "104",
        "n": [

        ]
      },
      {
        "level": 2,
        "id": "105",
        "n": [
          {
            "level": 3,
            "id": "106",
            "n": [

            ]
          },
          {
            "level": 3,
            "id": "107",
            "n": [
              {
                "level": 4,
                "id": "108",
                "n": [
                  {
                    "level": 5,
                    "id": "109",
                    "n": [

                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "level": 1,
    "id": "110",
    "n": [
      {
        "level": 2,
        "id": "111",
        "n": [
          {
            "level": 3,
            "id": "112",
            "n": [

            ]
          }
        ]
      },
      {
        "level": 2,
        "id": "113",
        "n": [
          {
            "level": 3,
            "id": "114",
            "n": [
              {
                "level": 4,
                "id": "115",
                "n": [

                ]
              }
            ]
          },
          {
            "level": 3,
            "id": "116",
            "n": [
              {
                "level": 4,
                "id": "117",
                "n": [

                ]
              },
              {
                "level": 4,
                "id": "118",
                "n": [
                  {
                    "level": 5,
                    "id": "119",
                    "n": [

                    ]
                  },
                  {
                    "level": 5,
                    "id": "120",
                    "n": [

                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

OUTPUT:

[
  {
    "level": 1,
    "id": "101",
    "n": [
      {
        "level": 2,
        "id": "102"
      }
    ]
  },
  {
    "level": 1,
    "id": "103",
    "n": [
      {
        "level": 2,
        "id": "104"
      },
      {
        "level": 2,
        "id": "105",
        "n": [
          {
            "level": 3,
            "id": "106"
          },
          {
            "level": 3,
            "id": "107",
            "n": [
              {
                "level": 4,
                "id": "108",
                "n": [
                  {
                    "level": 5,
                    "id": "109"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "level": 1,
    "id": "110",
    "n": [
      {
        "level": 2,
        "id": "111",
        "n": [
          {
            "level": 3,
            "id": "112"
          }
        ]
      },
      {
        "level": 2,
        "id": "113",
        "n": [
          {
            "level": 3,
            "id": "114",
            "n": [
              {
                "level": 4,
                "id": "115"
              }
            ]
          },
          {
            "level": 3,
            "id": "116",
            "n": [
              {
                "level": 4,
                "id": "117"
              },
              {
                "level": 4,
                "id": "118",
                "n": [
                  {
                    "level": 5,
                    "id": "119"
                  },
                  {
                    "level": 5,
                    "id": "120"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

All the ,"n":[] removed from the json array. Please help me with some PHP code to get the output from that above input.

  • 写回答

1条回答 默认 最新

  • dqw7121 2017-07-20 13:31
    关注

    Use the recursion, to recursively find the property n which has the value [] and delete them:

    function removeEmptyNode(nodes) {
      if(!nodes || nodes.length === 0)
        return;
      
      nodes.forEach(function(node) {
        if(node.n.length === 0) {
         delete node.n; 
        } else {
          removeEmptyNode(node.n);
        }
      });
    }
    
    
    var data = [{
      "level": 1,
      "id": "101",
      "n": [{
        "level": 2,
        "id": "102",
        "n": []
      }]
    }, {
      "level": 1,
      "id": "103",
      "n": [{
        "level": 2,
        "id": "104",
        "n": []
      }, {
        "level": 2,
        "id": "105",
        "n": [{
          "level": 3,
          "id": "106",
          "n": []
        }, {
          "level": 3,
          "id": "107",
          "n": [{
            "level": 4,
            "id": "108",
            "n": [{
              "level": 5,
              "id": "109",
              "n": []
            }]
          }]
        }]
      }]
    }, {
      "level": 1,
      "id": "110",
      "n": [{
        "level": 2,
        "id": "111",
        "n": [{
          "level": 3,
          "id": "112",
          "n": []
        }]
      }, {
        "level": 2,
        "id": "113",
        "n": [{
          "level": 3,
          "id": "114",
          "n": [{
            "level": 4,
            "id": "115",
            "n": []
          }]
        }, {
          "level": 3,
          "id": "116",
          "n": [{
            "level": 4,
            "id": "117",
            "n": []
          }, {
            "level": 4,
            "id": "118",
            "n": [{
              "level": 5,
              "id": "119",
              "n": []
            }, {
              "level": 5,
              "id": "120",
              "n": []
            }]
          }]
        }]
      }]
    }];
    
    removeEmptyNode(data);
    console.log(data);


    EDIT:

    If you want to do this in PHP, you can convert the above code easily to PHP.
    Assuming you have source object in JSON string, you could be able to use this code:

    <?php
    
    function removeEmptyNodes(&$nodes) {
        if(empty($nodes)) {
            return;
        }
    
        foreach($nodes as &$node) {
            if(empty($node['n'])) {
                unset($node['n']);
            } else {
                removeEmptyNodes($node['n']);
            }
        }
    }
    
    $json = '[{"level":1,"id":"101","n":[{"level":2,"id":"102","n":[]}]},{"level":1,"id":"103","n":[{"level":2,"id":"104","n":[]},{"level":2,"id":"105","n":[{"level":3,"id":"106","n":[]},{"level":3,"id":"107","n":[{"level":4,"id":"108","n":[{"level":5,"id":"109","n":[]}]}]}]}]},{"level":1,"id":"110","n":[{"level":2,"id":"111","n":[{"level":3,"id":"112","n":[]}]},{"level":2,"id":"113","n":[{"level":3,"id":"114","n":[{"level":4,"id":"115","n":[]}]},{"level":3,"id":"116","n":[{"level":4,"id":"117","n":[]},{"level":4,"id":"118","n":[{"level":5,"id":"119","n":[]},{"level":5,"id":"120","n":[]}]}]}]}]}]';
    
    $data = json_decode($json, true);
    
    removeEmptyNodes($data);
    print_r(json_encode($data));
    ?>
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 设计数字电路得到要求结果
  • ¥15 hfss 组天线阵列报错:The A and B vectors must be parallel to the XY plane,如何解决?
  • ¥30 ansible连接设备报错
  • ¥60 pmsampsize包取值问题
  • ¥15 波形合成电路的设计学习
  • ¥15 unity脚本挂不上
  • ¥15 数学建模数学建模需要
  • ¥20 Java Sound Api 调用Mixer.getLine偶现Line unsupported错误。
  • ¥15 使用cuda加速opencv运算但是报错AttributeError: module 'cv2.cuda' has no attribute 'getCudaEnabledDeviceCount'
  • ¥15 java输入输出异常