我需要将此空间树的嵌套集结构(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;
}