在php中设置一个带有子节点的数组



我从一个表中得到以下数据:

+----+---------+-----+---------+--------------+
| id | name    | id2 | sname   | relation     |
+----+---------+-----+---------+--------------+
|  1 | Albaraa |   2 | Brandon | Friend       |
|  1 | Albaraa |   3 | Shen    | Friend       |
|  1 | Albaraa |   4 | Dan     | Professor    |
|  1 | Albaraa |   5 | Bob     | Boss         |
|  1 | Albaraa |   6 | Al      | God Father   |
|  2 | Brandon |   5 | Bob     | Friend       |
|  3 | Shen    |   1 | Albaraa | Friend       |
|  3 | Shen    |   2 | Brandon | Friend       |
|  3 | Shen    |   4 | Dan     | Professor    |
|  4 | Dan     |   1 | Albaraa | Student      |
|  4 | Dan     |   2 | Brandon | Student      |
|  4 | Dan     |   3 | Shen    | Student      |
|  5 | Bob     |   6 | Al      | Best Friend  |
|  6 | Al      |   1 | Albaraa | God Son      |
|  6 | Al      |   5 | Bob     | Best Friends |
+----+---------+-----+---------+--------------+

我得到php数组格式的数据:

Array ( 
[0] => Array ( [id] => 1 [name] => Albaraa [id2] => 2 [sname] => Brandon [relation] => Friend ) 
[1] => Array ( [id] => 1 [name] => Albaraa [id2] => 3 [sname] => Shen [relation] => Friend ) 
[2] => Array ( [id] => 1 [name] => Albaraa [id2] => 4 [sname] => Dan [relation] => Professor ) 
[3] => Array ( [id] => 1 [name] => Albaraa [id2] => 5 [sname] => Bob [relation] => Boss ) 
[4] => Array ( [id] => 1 [name] => Albaraa [id2] => 6 [sname] => Al [relation] => God Father ) 
[5] => Array ( [id] => 2 [name] => Brandon [id2] => 5 [sname] => Bob [relation] => Friend ) 
[6] => Array ( [id] => 3 [name] => Shen [id2] => 1 [sname] => Albaraa [relation] => Friend ) 
[7] => Array ( [id] => 3 [name] => Shen [id2] => 2 [sname] => Brandon [relation] => Friend ) 
[8] => Array ( [id] => 3 [name] => Shen [id2] => 4 [sname] => Dan [relation] => Professor ) 
[9] => Array ( [id] => 4 [name] => Dan [id2] => 1 [sname] => Albaraa [relation] => Student ) 
[10] => Array ( [id] => 4 [name] => Dan [id2] => 2 [sname] => Brandon [relation] => Student ) 
[11] => Array ( [id] => 4 [name] => Dan [id2] => 3 [sname] => Shen [relation] => Student ) 
[12] => Array ( [id] => 5 [name] => Bob [id2] => 6 [sname] => Al [relation] => Best Friend ) 
[13] => Array ( [id] => 6 [name] => Al [id2] => 1 [sname] => Albaraa [relation] => God Son ) 
[14] => Array ( [id] => 6 [name] => Al [id2] => 5 [sname] => Bob [relation] => Best Friends ) )

我现在需要做的是将数据设置到一个数组中,该数组添加了第一个人,并从那里添加了他的孩子和孩子的孩子,它看起来像这样(使它看起来更容易读=]):

Array ( 
[id] => 0 
[name] => Albaraa 
[children] => Array ( 
    [0] => Array ( 
    [id] => 1 
    [name] => Brandon 
    [data] => Array ( 
        [relationTo] => Albaraa 
        [relation] => Friend ) 
    [children] => Array (
        [id] => 2 
        [name] => Bob 
        [data] => Array ( 
            [relationTo] => Brandon 
            [relation] => Friend )
         [children] => Array()
    )
    ...and so on
))

我有它的地方,它输出上面的,但没有孩子有任何孩子(只是一个空数组)我真的不确定如何让它正确做到这一点,我已经尝试过一个类做到这一点,但失败了…所以任何关于如何让数据看起来像上面一个的帮助将非常感激!

这里有一种PHP方法可以做到这一点,根本没有进行太多优化。您可能也可以使用SQL连接来获得一个看起来非常接近结果的数组。

// set root stuff
$person1 = $arr[0];
$new_arr = [];
$new_arr['id'] = 0;
$new_arr['name'] = $arr['name'];
// find children
foreach ($arr as $child){
    // if found child
    if ($child['sname'] == $person1['name']){
        // set data
        $child_arr = [];
        $child_arr['id'] = $child['id'];
        $child_arr['name'] = $child['name'];
        $child_arr['data']['relationTo'] = $child['sname'];
        $child_arr['data']['relation'] = $child['relation'];
        $new_arr['children'][] = $child_arr;
        // find grandchildren
        foreach ($arr as $gc){
            // if found grandchild
            if ($gc['sname'] == $child['name']){
                // set data
                $gc_arr = [];
                $gc_arr['id'] = $gc['id'];
                $gc_arr['name'] = $gc['name'];
                $gc_arr['data']['relationTo'] = $gc['sname'];
                $gc_arr['data']['relation'] = $gc['relation'];
                $gc_arr['children'] = []; // assuming you didn't want to go deeper
                $child_arr['children'][] = $gc_arr;
            }
        }
    }
}

如果你想更深入,我们需要引入递归

相关内容

  • 没有找到相关文章

最新更新