【发布时间】: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