我试图从表中检索分层数据,但是我没有这样做。该表(现在)具有以下列:ifc_key,ifc_name,ifc_parent。不使用ifc_key。 (主键,但不用于此功能)从递归函数中检索分层数据(Zend Framework)
目的是获取一个数组,每个元素都是一个“父”接口(因此所有这些根元素都是没有ifc_parent集的ifc_name值等于ifc_name如果设置)
考虑以下布局(演示):
ifc_key | ifc_name | ifc_parent
0 | parent_ifc |
1 | A0A | parent_ifc
2 | A0B | parent_ifc
3 | b0a | vif1
4 | b0b | vif1
5 | vif1 | A0A
所以我在寻找的阵列,从查询生成的是:
Array
(
[parent_ifc] => Array
(
[a0a] => Array
(
[vif1] => Array
(
[0] => b0a
[1] => b0b
)
)
[a0b] =>
)
)
我想出了这个功能是本段的下面。我想创建一个递归函数,该函数在查找孩子时调用它自己,但问题是在首次调用此方法时没有选择任何孩子。 (那些有空父母的人本身就是父母)。所以我只能让父母回来,但没有一个孩子(也可能是他们的孩子等 - 这在理论上可能是无限的)。
public static function getByFilerOrganisedChildren($filer_id, $parent = '')
{
$table = new Filer_Interface_Table();
$where[] = $table->getAdapter()->quoteInto('ifc_system_id = ?', $filer_id);
$where[] = $table->getAdapter()->quoteInto('ifc_parent = ?', $parent);
$rows = $table->fetchAll($where, 'ifc_parent ASC');
foreach ($rows as $row) {
if ($row->ifc_parent == '') $data[] = $row->ifc_name;
else {
$data[$row->ifc_parent][] = $row->ifc_name;
self::getByFilerOrganisedChildren($filer_id, $row->ifc_parent);
}
}
return (isset($data) ? $data : false);
}
$接口= Filer_Interface :: getByFilerOrganisedChildren($ filer-> SYSTEM_ID); – Tom 2010-03-04 07:58:28