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

时间:2017-07-20 13:23:35

标签: javascript php jquery json codeigniter

我在json数组下方,我想删除子节点" n":[] - 当它为空时。 (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": [

                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

输出:

[
  {
    "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"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

从json数组中删除所有" n":[]。 请帮我一些PHP代码,以获得上述输入的输出。

1 个答案:

答案 0 :(得分:0)

使用递归,以递归方式查找值为n的属性[]并删除它们:



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);




编辑:

如果你想在PHP中这样做,你可以轻松地将上面的代码转换为PHP 假设您在JSON字符串中有源对象,您可以使用此代码:

<?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));
?>