FLEA/FLEA/Helper/Array.php源代码如下:
function array_to_tree($arr, $fid, $fparent = 'parent_id', $fchildrens = 'childrens', $returnReferences = false) { $pkvRefs = array(); foreach ($arr as $offset => $row) { $pkvRefs[$row[$fid]] =& $arr[$offset]; } $tree = array(); foreach ($arr as $offset => $row) { $parentId = $row[$fparent]; if ($parentId) { if (!isset($pkvRefs[$parentId])) {continue; } $parent =& $pkvRefs[$parentId];//1 $parent[$fchildrens][] =& $arr[$offset]; //2 } else { $tree[] =& $arr[$offset]; } } if ($returnReferences) { return array('tree' => $tree, 'refs' => $pkvRefs); } else { return $tree; } }
加注释的这两行有错误...里面 $parent 操作之后未进行其它的操作,估计是笔误,将其改为 $tree即可更改后的代码如下
/** * 将一个平面的二维数组按照指定的字段转换为树状结构 * * 当 $returnReferences 参数为 true 时,返回结果的 tree 字段为树,refs 字段则为节点引用。 * 利用返回的节点引用,可以很方便的获取包含以任意节点为根的子树。 * * @param array $arr 原始数据 * @param string $fid 节点ID字段名 * @param string $fparent 节点父ID字段名 * @param string $fchildrens 保存子节点的字段名 * @param boolean $returnReferences 是否在返回结果中包含节点引用 * * return array */ function array_to_tree($arr, $fid, $fparent = 'parent_id', $fchildrens = 'childrens', $returnReferences = false) { $pkvRefs = array(); foreach ($arr as $offset => $row) { $pkvRefs[$row[$fid]] =& $arr[$offset]; } $tree = array(); foreach ($arr as $offset => $row) { $parentId = $row[$fparent]; if ($parentId) { if (!isset($pkvRefs[$parentId])) {continue; } $tree =& $pkvRefs[$parentId]; $tree[$fchildrens][] =& $arr[$offset]; } else { $tree[] =& $arr[$offset]; } } if ($returnReferences) { return array('tree' => $tree, 'refs' => $pkvRefs); } else { return $tree; } }
修改之后,结果正确,否则基本均为空数组