【问题标题】:Nested sets, php array and transformation嵌套集、php 数组和转换
【发布时间】:2009-05-08 17:46:47
【问题描述】:

我需要将我的嵌套集合结构 (mysql) 转换为这个空间树的 json 1)http://blog.thejit.org/wp-content/jit-1.0a/examples/spacetree.html

我发现这个函数可以从嵌套集合中创建一个数组: 2)http://semlabs.co.uk/journal/converting-nested-set-model-data-in-to-multi-dimensional-arrays-in-php

我也可以用php函数json_encode将php数组转换成json

我的问题:函数nestify(来自第二个链接)给我的并不完全是我需要的。我需要这样的东西:http://pastebin.com/m68752352

你能帮我改变“嵌套”函数,让它给我正确的数组吗?

这里再介绍一次这个函数:

function nestify( $arrs, $depth_key = 'depth' )
    {
        $nested = array();
        $depths = array();

        foreach( $arrs as $key => $arr ) {
            if( $arr[$depth_key] == 0 ) {
                $nested[$key] = $arr;
                $depths[$arr[$depth_key] + 1] = $key;
            }
            else {
                $parent =& $nested;
                for( $i = 1; $i <= ( $arr[$depth_key] ); $i++ ) {
                    $parent =& $parent[$depths[$i]];
                }

                $parent[$key] = $arr;
                $depths[$arr[$depth_key] + 1] = $key;
            }
        }

        return $nested;
    }

【问题讨论】:

    标签: php arrays nested-sets


    【解决方案1】:

    下面的 sn-p 应该可以解决问题,改编自我在网上找到的一些 PHP Doctrine 代码:

    function toHierarchy($collection)
    {
            // Trees mapped
            $trees = array();
            $l = 0;
    
            if (count($collection) > 0) {
                    // Node Stack. Used to help building the hierarchy
                    $stack = array();
    
                    foreach ($collection as $node) {
                            $item = $node;
                            $item['children'] = array();
    
                            // Number of stack items
                            $l = count($stack);
    
                            // Check if we're dealing with different levels
                            while($l > 0 && $stack[$l - 1]['depth'] >= $item['depth']) {
                                    array_pop($stack);
                                    $l--;
                            }
    
                            // Stack is empty (we are inspecting the root)
                            if ($l == 0) {
                                    // Assigning the root node
                                    $i = count($trees);
                                    $trees[$i] = $item;
                                    $stack[] = & $trees[$i];
                            } else {
                                    // Add node to parent
                                    $i = count($stack[$l - 1]['children']);
                                    $stack[$l - 1]['children'][$i] = $item;
                                    $stack[] = & $stack[$l - 1]['children'][$i];
                            }
                    }
            }
    
            return $trees;
    }
    

    【讨论】:

    • 谁能告诉我&amp; 的作用,例如$stack[] = &amp; $trees[$i];
    • 它将树项的引用存储到堆栈中(这只是指向当前对象的指针,而不是它的副本)。有关更多信息,您可以阅读 PHP 手册中的 References Explained 章节。
    • 重要的事情。要使用此功能,您需要通过left_id(它是嵌套集合结构的服务字段)列应用collection的订购项!
    猜你喜欢
    • 2017-01-29
    • 2017-10-11
    • 2015-10-23
    • 2018-06-06
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多