嵌套套,PHP数组和改造 [英] Nested sets, php array and transformation

查看:193
本文介绍了嵌套套,PHP数组和改造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要我的嵌套组结构(MySQL的)转换成JSON这个spacetree
1) http://blog.thejit.org/wp-内容/ JIT-1.0A /例子/ spacetree.html

我发现这个功能来创建嵌套集合数组:
2)http://semlabs.co.uk/journal/converting-nested-set-model-data-in-to-multi-dimensional-arrays-in-php

我还可以将PHP数组成JSON与PHP函数json_en code

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

您可以帮我换功能nestify,所以它给了我正确的阵列?

下面是这个函数,我们有更多的时间:

 函数nestify($ ARRS,$ depth_key =深度)
    {
    $嵌套=阵列();
    $深度=阵列();    的foreach($ ARRS为$关键=> $ ARR){
    如果($改编[$ depth_key] == 0){
    $嵌套[$关键] = $编曲;
    $深处[$改编[$ depth_key] + 1] = $键;
    }
    其他{
    $父=安培; $嵌套;
    为($ i = 1; $ I< =($改编[$ depth_key]); $ I ++){
    $父=安培; $父[$深处[$ i]];
    }    $父[$关键] = $编曲;
    $深处[$改编[$ depth_key] + 1] = $键;
    }
    }    返回$嵌套;
    }


解决方案

下面的代码片段应该做的伎俩,改编自一些PHP主义code我在网上找到:

 函数toHierarchy($集合)
{
        //树映射
        $树=阵列();
        $ L = 0;        如果(计数($集合)大于0){
                //节点堆栈。用来帮助建立等级
                $堆栈=阵列();                的foreach($收藏为$节点){
                        $项目= $节点;
                        $项目['孩子'] =阵列();                        //堆栈数项
                        $ L =计数($堆栈);                        //检查是否我们正在处理的不同级别
                        而($ L> 0安培;&安培; $栈[$ L - 1] ['深度']> = $项目['深度']){
                                array_pop($堆栈);
                                $ L--;
                        }                        //堆栈是空的(我们正在检查的根)
                        如果($ L == 0){
                                //分配根节点
                                $ I = COUNT($树);
                                $树木[$ i] = $项目;
                                $堆叠[] =&放大器; $树木[$ i];
                        }其他{
                                //添加节点到父
                                $ I = COUNT($栈[$ L - 1] ['孩子']);
                                $栈[$ L - 1] ['孩子'] [$ i] = $项目;
                                $堆叠[] =&放大器; $栈[$ L - 1] ['孩子'] [$ i];
                        }
                }
        }        返回$树木;
}

I need to transform my nested sets structure (mysql) into json for this spacetree 1) http://blog.thejit.org/wp-content/jit-1.0a/examples/spacetree.html

I found this function to create an array from nested sets: 2) http://semlabs.co.uk/journal/converting-nested-set-model-data-in-to-multi-dimensional-arrays-in-php

I can also convert php array into json with PHP function json_encode

My problem: the function nestify (from second link) gives me not exactly that i need. I need something like this: http://pastebin.com/m68752352

Can you help me change the function "nestify" so it gives me the correct array?

Here is this function one more time:

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

解决方案

The following snippet should do the trick, adapted from some PHP Doctrine code I found on the web :

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

这篇关于嵌套套,PHP数组和改造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆