以递归方式查找parent_id并将多维数组转换为一维

时间:2014-09-09 03:53:45

标签: php recursion multidimensional-array

我想转换一个多维数组,如:

array (size=3)
  0 => 
    object(stdClass)[853]
      public 'id' => int 1
      public 'children' => 
        array (size=5)
          0 => 
            object(stdClass)[852]
              public 'id' => int 31
              public 'children' => 
                array (size=2)
                  0 => 
                    object(stdClass)[854]
                      public 'id' => int 32
                  1 => 
                    object(stdClass)[855]
                      public 'id' => int 36
          1 => 
            object(stdClass)[856]
              public 'id' => int 4
          2 => 
            object(stdClass)[857]
              public 'id' => int 30
          3 => 
            object(stdClass)[858]
              public 'id' => int 8
          4 => 
            object(stdClass)[859]
              public 'id' => int 5
              public 'children' => 
                array (size=1)
                  0 => 
                    object(stdClass)[860]
                      public 'id' => int 7
  1 => 
    object(stdClass)[861]
      public 'id' => int 2
      public 'children' => 
        array (size=8)
          0 => 
            object(stdClass)[862]
              public 'id' => int 14
              public 'children' => 
                array (size=7)
                  0 => 
                    object(stdClass)[863]
                      public 'id' => int 16
                  1 => 
                    object(stdClass)[864]
                      public 'id' => int 20
                  2 => 
                    object(stdClass)[865]
                      public 'id' => int 15
                  3 => 
                    object(stdClass)[866]
                      public 'id' => int 29
                  4 => 
                    object(stdClass)[867]
                      public 'id' => int 19

类似于:

[
    'id'        => 1,
    'parent_id' => 0
],
[
    'id'        => 31,
    'parent'    => 1
],
[
    'id'        => 32,
    'parent'    => 31
],
[
    'id'        => 36,
    'parent'    => 31   
],
[
    'id'        => 4,
    'parent'    => 1
],
[
    'id'        => 30,
    'parent'    => 1
],
[
    'id'        => 8,
    'parent'    => 1
],
[
    'id'        => 5,
    'parent'    => 1
],
[
    'id'        => 7,
    'parent'    => 5
],
[
    'id'        => 2,
    'parent'    => 0
],
[
    'id'        => 14,
    'parent'    => 2
],
[
    'id'        => 16,
    'parent'    => 14
],
[
    'id'        => 20,
    'parent'    => 14
]

但我找不到办法做到这一点。 我试过很多方法,比如:

public function linksRecursive($array, $res = [], $parent = 0)
{
    foreach($array as $arr)
    {
        $res[] = [
            'id' => $arr->id,
            'parent' => $parent
        ];
        // echo 'ID: ' . $arr->id . ', PARENT: ' . $parent . "<br>";
        $parent = $arr->id;
        if(isset($arr->children))
        {
            $this->linksRecursive($arr->children, $res, $parent);
        }
    }

    return $res;

}

我该怎么做才能解决这个问题?有办法做点什么吗? 我正在使用序列化的可排序/可嵌套的JQuery ui插件,我构建它以将新订单保存到我的菜单中。

2 个答案:

答案 0 :(得分:0)

非常简单的错误,你需要设置$ res作为你的linkRecursive方法的结果。 只是做...

        $res = $this->linksRecursive($arr->children, $res, $parent);

你应该没事。

答案 1 :(得分:0)

感谢答案!

由于你的答案,我解决了这个问题!

public function linksRecursive(array $array, &$res = [], $parent = 0)
{
    foreach($array as $arr)
    {
        $res[] = [
            'id' => $arr->id,
            'parent' => $parent
        ];
        if(isset($arr->children))
        {
            $this->linksRecursive($arr->children, $res, $arr->id);
        }

    }

    return $res;

}