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 zabbix6.4与frp如何进行联动
  • ¥15 如何使用Echarts制作途中时间序列表
  • ¥15 图论相关的数学问题,共10个
  • ¥15 EtherCAT的问题,创建一个XML文件
  • ¥15 微信公众号羽毛球自动抢定场程序制作
  • ¥15 Fluent UDF 编写
  • ¥15 xcode开发的macos应用,如何使窗口居中显示?
  • ¥20 统信uos升级apt后启动失败
  • ¥15 求指导储层饱和度及含水率测井解释
  • ¥200 建三维地震工区写入sgy到指定目录