PHP嵌套数组转换为平面数组

时间:2017-05-12 10:29:07

标签: php multidimensional-array flatten

我有一个问题就是像这样转换我的分层数组:

array(
[0]=>
  array(3) {
    ["id"]=>
    int(2353011010)
    ["name"]=>
    string(17) "LEDER ACCESSOIRES"
    ["order"]=>
    int(15)
  }
[1]=>
  array(3) {
    ["id"]=>
    int(2371475010)
    ["name"]=>
    string(15) "SPORT AUFKLEBER"
    ["order"]=>
    int(25)
  }
[2]=>
  array(4) {
    ["id"]=>
    int(2563635010)
    ["name"]=>
    string(17) "KENNZEICHENHALTER"
    ["order"]=>
    int(10)
    ["children"]=>
    array(6) {
      [0]=>
      array(4) {
        ["id"]=>
        int(3854259010)
        ["name"]=>
        string(9) "EDELSTAHL"
        ["order"]=>
        int(92)
        ["children"]=>
        array(2) {
          [0]=>
          array(3) {
            ["id"]=>
            int(20878056010)
            ["name"]=>
            string(5) "test1"
            ["order"]=>
            int(1)
          }
        }
      }
      [1]=>
      array(3) {
        ["id"]=>
        int(3854260010)
        ["name"]=>
        string(5) "CHROM"
        ["order"]=>
        int(91)
      }
    }
[3]=>
  array(4) {
    ["id"]=>
    int(19754330010)
    ["name"]=>
    string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
    ["order"]=>
    int(3)
    }
}
)

进入这样的平面:

array(
[0]=>
    array(3) {
        ["id"]=>
        int(2353011010)
        ["name"]=>
        string(17) "LEDER ACCESSOIRES"
        ["order"]=>
        int(15)
    }
[1]=>
    array(3) {
        ["id"]=>
        int(2371475010)
        ["name"]=>
        string(15) "SPORT AUFKLEBER"
        ["order"]=>
        int(25)
    }
[2]=>
    array(3) {
        ["id"]=>
        int(2563635010)
        ["name"]=>
        string(17) "KENNZEICHENHALTER"
        ["order"]=>
        int(10)
    }
[3]=>
    array(4) {
        ["id"]=>
        int(3854259010)
        ["name"]=>
        string(9) "EDELSTAHL"
        ["order"]=>
        int(92),
        ["parentId"]=> 2563635010
    }
[4]=>
    array(4) {
        ["id"]=>
        int(20878056010)
        ["name"]=>
        string(5) "test1"
        ["order"]=>
        int(1),
        ["parentId"]=> 2563635010
    }
[5]=>
    array(4) {
        ["id"]=>
        int(3854260010)
        ["name"]=>
        string(5) "CHROM"
        ["order"]=>
        int(91),
        ["parentId"]=> 2563635010
    }
[6]=>
  array(4) {
    ["id"]=>
    int(19754330010)
    ["name"]=>
    string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
    ["order"]=>
    int(3)
}
)

应该删除子元素,并且每个子元素都应该获得更高级别id的parentId实体。我需要这个解决方案来转移到DB。

THX

1 个答案:

答案 0 :(得分:0)

现在,我已经创建了一个适合我的“临时”方法,但使用起来非常灵活:

function recursive($categories) {

    foreach ($categories as $value) {
        $result[$value->id]['id'] = $value->id;
        $result[$value->id]['name'] = $value->name;
        $result[$value->id]['order'] = $value->order;
        $result[$value->id]['parentId'] = 0;

        if(isset($value->children)) {
            $parentId = $value->id;

            foreach($value->children as $value2) {
                $result[$value2->id]['id'] = $value2->id;
                $result[$value2->id]['name'] = $value2->name;
                $result[$value2->id]['parentId'] = $parentId;

                if(isset($value2->children)) {
                    $parentId = $value2->id;

                    foreach($value2->children as $value3) {
                        $result[$value3->id]['id'] = $value3->id;
                        $result[$value3->id]['name'] = $value3->name;
                        $result[$value3->id]['parentId'] = $parentId;
                    }
                }
            }
        }
    }

    return $result;
}

有人知道这种方法的递归解决方案吗?