PHP删除无用的嵌套数组

时间:2016-08-19 10:34:39

标签: php arrays removechild

我正在使用jQuery插件生成以下数组:

    array:4 [
  0 => array:1 [
    "page_id" => "1"
  ]
  1 => array:2 [
    "page_id" => "2"
    "children" => array:1 [
      0 => array:2 [
        0 => array:2 [
          "page_id" => "10"
          "children" => array:1 [
            0 => array:2 [
              0 => array:1 [
                "page_id" => "12"
              ]
              1 => array:1 [
                "page_id" => "13"
              ]
            ]
          ]
        ]
        1 => array:1 [
          "page_id" => "11"
        ]
      ]
    ]
  ]
  2 => array:1 [
    "page_id" => "4"
  ]
  3 => array:1 [
    "page_id" => "3"
  ]
]

此数组是动态的,因此可以有更多嵌套数组。我需要清理一点阵列。你可以在page_id =>下看到2有孩子。它包含一个无用的数组(0 =>数组:2)。

是否可以删除这个无用的数组?我真的需要它来重新生成我的菜单项位置......有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您可以使用递归算法:

function removeUselessArrays($array) {
    $newArray = [];

    foreach ($array as $key => $value) {
        if (is_array($value)) {
            if (array_keys($value) === [ 0 ]) {
                $newArray[$key] = removeUselessArrays($value[0]);
            } else {
                $newArray[$key] = removeUselessArrays($value);
            }
        } else {
            $newArray[$key] = $value;
        }
    }

    return $newArray;
}

查看正在使用的代码here

答案 1 :(得分:0)

你可以在循环中这样做:

  

<强>算法:

<?php

    foreach($arr as $key=>$val){
        foreach($val as $index=>$item){
            if( is_array($item) ){
                $children               = ["page_id"=>$item[0]["page_id"]];
                $children               = array_merge($children, $item[0][$index]);
                $arr[$key][$index]      = $children;
            }
        }
    }

<强> TEST

    <?php

    // LOOP THROUGH THE ARRAY CONTAINING UNWANTED ELEMENT(S)
    foreach($arr as $key=>$val){
        foreach($val as $index=>$item){
            // CHECK IF ONE OF THE SUB-ELEMENTS IS AN ARRAY & HAS KEY: children
            if( is_array($item) ){
                // IF SO; GET THE CHILDREN ELEMENT OFF THAT ITEM
                // AND STORE IT IN A VARIABLE...
                $children               = $item[0][$index];
                // NOW SET THE SUB-ELEMENT TO THE $children
                $children               = ["page_id"=>$item[0]["page_id"]];
                $children               = array_merge($children, $item[0][$index]);
                $arr[$key][$index]      = $children;
            }
        }
    }

    // DUMP-OUT THE OUTPUT:
    var_dump($arr);

    // PRODUCES:::  
    array (size=4)
      0 =>
        array (size=1)
          'page_id' => string '1' (length=1)
      1 =>
        array (size=2)
          'page_id' => string '2' (length=1)
          'children' =>
             array (size=3)
              'page_id' => string '10' (length=1)
               0 =>
                array (size=1)
                  'page_id' => string '12' (length=1)
               1 =>
                array (size=1)
                  'page_id' => string '13' (length=1)
      2 =>
        array (size=1)
          'page_id' => string '4' (length=1)
      3 =>
        array (size=1)
          'page_id' => string '3' (length=1)

找到快速测试Here