0

我有一个表中的以下数据:

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

我在它输出上述内容的地方有它,但没有孩子有任何孩子(只是一个空数组)我不确定如何让它正确地做到这一点,我已经尝试过一个类来做到这一点但失败了。 ..因此,任何有关如何使数据看起来像上述数据的帮助将不胜感激!

4

1 回答 1

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;
            }
        }
    }
}

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

于 2013-05-03T05:23:44.073 回答