在php中使用子节点设置数组

时间:2013-05-03 05:04:21

标签: php mysql sql arrays multidimensional-array

我从表中得到以下数据:

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

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

1 个答案:

答案 0 :(得分:0)

这是一种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;
            }
        }
    }
}

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