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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器