php实现无限级分类,php实现级分类
复制代码 代码如下:
$area = array(
array('id'=>1,'name'=>'安徽','parent'=>0),
array('id'=>2,'name'=>'海淀','parent'=>7),
array('id'=>3,'name'=>'濉溪县','parent'=>5),
array('id'=>4,'name'=>'昌平','parent'=>7),
array('id'=>5,'name'=>'淮北','parent'=>1),
array('id'=>6,'name'=>'朝阳','parent'=>7),
array('id'=>7,'name'=>'北京','parent'=>0),
array('id'=>8,'name'=>'上地','parent'=>2)
);
1.递归,查找子孙树
复制代码 代码如下:
function subtree($arr,$id=0,$lev=1) {
$subs = array(); // 子孙数组
foreach($arr as $v) {
if($v['parent'] == $id) {
$v['lev'] = $lev;
$subs[] = $v; // 举例说找到array('id'=>1,'name'=>'安徽','parent'=>0),
$subs = array_merge($subs,subtree($arr,$v['id'],$lev+1));
}
}
return $subs;
}
$tree = subtree($area,0,1);
foreach($tree as $v) {
echo str_repeat(' ',$v['lev']),$v['name'],'
';
}
2.递归,求家谱树
家谱树的应用 ,如面包屑导航 首页 > 手机类型 > CDMA手机 > 公益PHP > 递归应用
复制代码 代码如下:
function familytree($arr,$id) {
$tree = array();
foreach($arr as $v) {
if($v['id'] == $id) {// 判断要不要找父栏目
if($v['parent'] > 0) { // parnet>0,说明有父栏目
$tree = array_merge($tree,familytree($arr,$v['parent']));
}
$tree[] = $v; // 以找到上地为例
}
}
return $tree;
}
print_r(familytree($area,8)); // 北京->海淀->上地
2.迭代,求家谱树
复制代码 代码如下:
// 迭代,效率比递归高,代码也没多.
// 找家谱树推荐用迭代
function tree($arr,$id) {
$tree = array();
while($id !== 0) {
foreach($arr as $v) {
if($v['id'] == $id) {
$tree[] = $v;
$id = $v['parent'];
break;
}
}
}
return $tree;
}
print_r(tree($area,8));

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
