dpw70180 2013-01-14 11:42
浏览 36

如何安排像菜单顺序的数组

I have an array which i would like to re-arrange sequentially parent to child. Here in my array got unique value 'table_id' & value 'parent_table'. So parent_table will check if there have any 'table_id' exist or not. if exist it will goes under that 'table_id'. Here is the code:

    Array
(
[0] => Array
(
[table_id] => 7
[table_name] => Macro
[parent_table] => 1
)
[1] => Array
(
[table_id] => 4
[table_name] => Dise
[parent_table] => 7
)
[2] => Array
(
[table_id] => 5
[table_name] => Cox
[parent_table] => 7
)
[3] => Array
(
[table_id] => 6
[table_name] => Ripo
[parent_table] => 4
)
)

Sample Output:

 Array
       (
         [1] => Array
                    (
                    [table_id] => 7
                    [table_name] => Macro
                    [parent_table] => 1
                    [7] => Array
                               (
                               [table_id] => 4
                               [table_name] => Dise
                               [parent_table] => 7
                               [4] => Array
                                          (
                                           [table_id] => 6
                                           [table_name] => Ripo
                                           [parent_table] => 4
                                           )
                                )
                     [7] => Array
                                 (
                                 [table_id] => 5
                                 [table_name] => Cox
                                 [parent_table] => 7
                                 )
               )
)

Please provide some idea to as i am very new to php.

  • 写回答

1条回答 默认 最新

  • dsvjmc0907 2013-01-14 13:02
    关注

    You can use a couple foreach loops and references:

    // Build a new array, with nodes indexed by table_id
    $byID = array();
    foreach ($arr as $node) {
        $byID[$node['table_id']] = $node;
    }
    
    // Append child nodes to their parents' child_tables arrays
    foreach ($byID as &$node) {
        if (isset($node['parent_table'])) {
            $byID[$node['parent_table']]['child_tables'][] =& $node;
        }
    }
    

    Then the entire tree is represented by $byID[1]:

    Array
    (
        [child_tables] => Array
            (
                [0] => Array
                    (
                        [table_id] => 7
                        [table_name] => Macro
                        [parent_table] => 1
                        [child_tables] => Array
                            (
                                [0] => Array
                                    (
                                        [table_id] => 4
                                        [table_name] => Dise
                                        [parent_table] => 7
                                        [child_tables] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [table_id] => 6
                                                        [table_name] => Ripo
                                                        [parent_table] => 4
                                                    )
    
                                            )
    
                                    )
    
                                [1] => Array
                                    (
                                        [table_id] => 5
                                        [table_name] => Cox
                                        [parent_table] => 7
                                    )
    
                            )
    
                    )
    
            )
    
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度