栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php 无限级分类学习参考之对ecshop无限级分类的解析 带详细注释

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php 无限级分类学习参考之对ecshop无限级分类的解析 带详细注释

复制代码 代码如下:
function cat_options($spec_cat_id, $arr)
{
static $cat_options = array();
if (isset($cat_options[$spec_cat_id]))
{
return $cat_options[$spec_cat_id];
}

if (!isset($cat_options[0]))
{
$level = $last_cat_id = 0;
$options = $cat_id_array = $level_array = array();
while (!empty($arr))//如果还有待构造的节点则继续遍历
{
foreach ($arr AS $key => $value)
{
$cat_id = $value['cat_id'];
//一级分类结点
if ($level == 0 && $last_cat_id == 0)
{
if ($value['parent_id'] > 0)
{
break;
}
$options[$cat_id] = $value;
$options[$cat_id]['level'] = $level;
$options[$cat_id]['id'] = $cat_id;
$options[$cat_id]['name'] = $value['cat_name'];
//遍历过了就不再遍历
unset($arr[$key]);
if ($value['has_children'] == 0)
{
continue;
}
$last_cat_id = $cat_id;//下层结点的父亲结点
$cat_id_array = array($cat_id);
$level_array[$last_cat_id] = ++$level;
continue;
}
//当前结点的父亲结点ID等于它的上一级结点ID
if ($value['parent_id'] == $last_cat_id)
{
$options[$cat_id] = $value;
$options[$cat_id]['level'] = $level;
$options[$cat_id]['id'] = $cat_id;
$options[$cat_id]['name'] = $value['cat_name'];
unset($arr[$key]);//遍历过了就不再遍历
//如果当前结点有孩子则当前结点要进驻,但不再遍历;反之不进驻也不再遍历
if ($value['has_children'] > 0)
{
if (end($cat_id_array) != $last_cat_id)
{
$cat_id_array[] = $last_cat_id;
}
$last_cat_id = $cat_id;//当现结点做为下一级结点的新的父亲结点
$cat_id_array[] = $cat_id;//进驻

$level_array[$last_cat_id] = ++$level;//当前结点的下一级结点深度
}

}
elseif ($value['parent_id'] > $last_cat_id)
{//如果当前结点父亲深度大于目前父亲结点的深度则进行下一轮循环
break;
}
}//endforeach
$count = count($cat_id_array);
if ($count > 1)
{
//取出最后进驻的父亲节点作为当前父亲节点
$last_cat_id = array_pop($cat_id_array);
}
elseif ($count == 1)
{
if ($last_cat_id != end($cat_id_array))
{
//进驻的父亲结点只有一个时并且没有作为当前父亲节点时把它取出
$last_cat_id = end($cat_id_array);
}
else
{ //否则最后取出的父亲结点一定是一级分类结点
$level = 0;
$last_cat_id = 0;
$cat_id_array = array();
continue;
}
}

if ($last_cat_id && isset($level_array[$last_cat_id]))
{
//取出当前结点的深度
$level = $level_array[$last_cat_id];
}
else
{
$level = 0;
}
}//end while,此时已完成非递归前序遍历构造树的工作,其中$options已保存了从根结点开始的所有结点带有分层性质的数组
$cat_options[0] = $options;
}
else
{
$options = $cat_options[0];
}
//如果从0开始即取整个树则直接返回不再处理.
if (!$spec_cat_id)
{
return $options;
}
//否则开始从指定结点截取,以下比较简单我还是稍微说说吧,要说就说几个参数含义吧

else
{
if (empty($options[$spec_cat_id]))
{
return array();
}
$spec_cat_id_level = $options[$spec_cat_id]['level'];

foreach ($options AS $key => $value)
{
if ($key != $spec_cat_id)
{
unset($options[$key]);
}
else
{
break;
}
}
$spec_cat_id_array = array();
foreach ($options AS $key => $value)
{
if (($spec_cat_id_level == $value['level'] && $value['cat_id'] != $spec_cat_id) ||
($spec_cat_id_level > $value['level']))
{
break;
}
else
{
$spec_cat_id_array[$key] = $value;
}
}
$cat_options[$spec_cat_id] = $spec_cat_id_array;
return $spec_cat_id_array;
}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/52261.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号