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

从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>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题