作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个包含一堆类别的数据库,有些是 child :
Array
(
[0] => Array
(
[id] => 1
[name] => Home Improvement
[slug] => Home-Improvement
[parent] =>
[user_id] => 1
[order] => 1
)
[1] => Array
(
[id] => 2
[name] => Asbestos Abatement & Removal
[slug] => Asbestos-Abatement-Removal
[parent] => 1
[user_id] => 1
[order] => 8
)
[2] => Array
(
[id] => 3
[name] => Asphalt & Asphalt Products
[slug] => Asphalt-Asphalt-Products
[parent] => 1
[user_id] => 1
[order] => 9
)
[3] => Array
(
[id] => 4
[name] => Bathroom
[slug] => Bathroom
[parent] => 1
[user_id] => 1
[order] => 10
)
[4] => Array
(
[id] => 5
[name] => Kitchen Cabinets
[slug] => Kitchen-Cabinets
[parent] => 1
[user_id] => 1
[order] => 11
)
[5] => Array
(
[id] => 6
[name] => Ceilings
[slug] => Ceilings
[parent] => 1
[user_id] => 1
[order] => 12
)
[6] => Array
(
[id] => 7
[name] => Cleaning
[slug] => Cleaning
[parent] => 1
[user_id] => 1
[order] => 13
)
[7] => Array
(
[id] => 8
[name] => Closet Organizers & Accessories
[slug] => Closet-Organizers-Accessories
[parent] => 1
[user_id] => 1
[order] => 14
)
[8] => Array
(
[id] => 9
[name] => Concrete
[slug] => Concrete
[parent] => 1
[user_id] => 1
[order] => 15
)
[9] => Array
(
[id] => 10
[name] => Contractors & Service Providers
[slug] => Contractors-Service-Providers
[parent] => 1
[user_id] => 1
[order] => 16
)
我想输出的是这样的:
<ul>
<li>Parent
<ul>
<li>Child</li>
</ul>
</li>
<li>Parent with no Children</li>
</ul>
我正在尝试用 PHP 构建一个递归树脚本,但我被卡住了。这是我到目前为止所拥有的。我坚持在 else: 和 endif 之间做什么;在foreach中。 (我使用该语法只是为了方便阅读。)有什么建议吗?
echo $this->categories->makeTree(0, $this->db->get('categories')->result_array());
public static function makeTree($parent, $array)
{
if (!is_array($array)) return '';
$output = '<ul>';
foreach($array as $key => $value):
if ($value['parent'] == $parent):
$output .= '<li>';
if ($value['parent'] == NULL):
$output .= $value['name'];
else:
endif;
endif;
$output .= '</li>';
endforeach;
$output .= '</ul>';
return $output;
}
编辑 1
虽然我在 foreach 循环中有一个数据库调用,但我能够让它工作,这可能不是最好的主意:
public function makeTree($parent, $array)
{
if (!is_array($array)) return FALSE;
$output = '<ul>';
foreach($array as $key => $value):
if ($value['parent'] == $parent):
$output .= '<li>';
if ($value['parent'] == NULL):
$output .= $value['name'];
$subcategories = ci()->db->get_where('categories', array('parent' => $value['id']));
if ($subcategories->num_rows() > 0):
$output .= $this->makeTree($value['id'], $subcategories->result_array());
endif;
else:
$output .= $value['name'];
$output .= '</li>';
endif;
endif;
endforeach;
$output .= '</ul>';
return $output;
}
编辑 2
这是我的最终解决方案,重用数组而不是进行数据库查询:
public function makeTree($parent, $array)
{
if (!is_array($array) OR empty($array)) return FALSE;
$output = '<ul>';
foreach($array as $key => $value):
if ($value['parent'] == $parent):
$output .= '<li>';
if ($value['parent'] == NULL):
$output .= $value['name'];
$matches = array();
foreach($array as $subkey => $subvalue):
if ($subvalue['parent'] == $value['id']):
$matches[$subkey] = $subvalue;
endif;
endforeach;
$output .= $this->makeTree($value['id'], $matches);
else:
$output .= $value['name'];
$output .= '</li>';
endif;
endif;
endforeach;
$output .= '</ul>';
return $output;
}
最佳答案
虽然这似乎得到了回答,但看看here .使用显示的函数,您只需一次迭代即可将平面数据转换为嵌套数据。然后从该嵌套数据创建一个 ul-list 就非常容易了。例如:
function nested2ul($data) {
$result = array();
if (sizeof($data) > 0) {
$result[] = '<ul>';
foreach ($data as $entry) {
$result[] = sprintf(
'<li>%s %s</li>',
$entry['name'],
nested2ul($entry['children'])
);
}
$result[] = '</ul>';
}
return implode($result);
}
echo nested2ul(array(flat2nested( $yourFlatData ));
这种方法的好处是您不必为了找到子元素而一次又一次地重复输入数据。
关于php - 创建递归类别树函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12285694/
我是一名优秀的程序员,十分优秀!